手机安全卫士06-手机防盗之自定义对话框

修改主界面的titleBar

可以在系统的AndroidManifest.xml文件中修改相应的配置来改变主界面的theme(设置为无titleBar样式)

当前主界面的样式为:


        <activity android:name="com.liuhao.mobilesafe.ui.MainActivity"
            android:theme="@android:style/Theme.NoTitleBar"
            android:label="@string/main_screen">
        </activity>

设置后样式为:

添加自定义的title,直接在主界面布局的最上方添加一个,其中添加相应的文字,如下:


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:background="@drawable/title_background"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/textcolor"
            android:textSize="22sp"
            android:text="山寨手机卫士"
            />
    </LinearLayout>

其中又添加了一个title_background的背景:


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <!-- 边框 -->
    <stroke
        android:width="0.5dip"
        android:color="#ff505050"
        />

    <!-- 指定边角 -->
    <corners
        android:radius="2dip"
        />

    <!-- 渐变色 -->
    <gradient
        android:startColor="#ff505050"
        android:centerColor="#ff383030"
        android:endColor="#ff282828"/>

</shape>

添加之后效果:


从主界面点击激活图切换到活手机防盗界面


@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Log.i(TAG, "点击的位置" + position);
        switch(position){
        case 0 :
            Log.i(TAG, "进入手机防盗");
            Intent lostIntent = new Intent(MainActivity.this, LostProtectedActivity.class);
            startActivity(lostIntent);
            break;
        }
    }

图标隐藏后,用户可以通过在拨号界面拨打某个号码进入手机防盗界面(知识点:broadcast)

要获取系统发送的广播

  • CallPhoneReceiver:广播接收者中,接收后进行相应的处理
  • 配置系统receiver AndroidManifest.xml:

    <receiver android:name="com.liuhao.mobilesafe.receiver.CallPhoneReceiver" >
        <intent-filter android:priority="1000">
            <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
        </intent-filter>
    </receiver>
  • 配置权限:android.permission.PROCESS_OUTGOING_CALLS,重新设置外拨电话的路径

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

异常处理:

未能激活目标Activity,程序异常终止。

Unable to start receiver com.liuhao.mobilesafe.receiver.CallPhoneReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really
what you want?

在一个Activity栈外调用startActivity()操作,必须为Intent显示的指定FLAG_ACTIVITY_NEW_TASK标志。

分析:我们是在一个广播接收者中激活一个Activity,Activity是运行在任务栈中的,而广播接收者则不在任务栈中。因此,若在一个广播接收者或者一个service中激活一个Activity必须指定FLAG_ACTIVITY_NEW_TASK标志,指定要激活的Activity在自己的任务栈中运行。


<pre name="code" class="java">lostIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

  • CallPhoneReceiver.java 完整的

package com.liuhao.mobilesafe.receiver;

import com.liuhao.mobilesafe.ui.LostProtectedActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class CallPhoneReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String number = getResultData();
        if("20122012".equals(number)){
            Intent lostIntent = new Intent(context, LostProtectedActivity.class);
            // 指定要激活的Activity在自己的任务栈中运行
            lostIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(lostIntent);
            // 终止这个拨号
            // 不能通过abortBroadcast()终止
            setResultData(null);
        }
    }

}

自己完成设置“暗号功能”

手机防盗界面

  1. 第一次进入时弹出对话框,让用户设置密码
  2. 设置完毕再次进入时弹出对话框,输入密码才能进入

实现自定义对话框

  • style.xml 其中实现了一个自定义对话框框架

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/title_background</item>
        <item name="android:windowNoTitle">true</item>
    </style>

</resources>
  • first_entry_dialog.xml 自定义对话框中的布局内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dip"
    android:layout_height="280dip"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置密码"
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="300dip"
        android:layout_height="180dip"
        android:background="#ffc8c8c8"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="设置手机防盗的密码"

            android:textColor="#ff000000" />

        <EditText
            android:id="@+id/et_first_entry_pwd"
            android:layout_width="300dip"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="再次输入密码"
            android:textColor="#ff000000" />

        <EditText
            android:id="@+id/et_first_entry_pwd_confirm"
            android:layout_width="300dip"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="300dip"
        android:layout_height="50dip"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:layout_width="140dip"
            android:layout_height="40dip"
            android:background="@drawable/button_background"
            android:text="确定"
            android:textColor="#ffffffff" />

        <Button
            android:layout_width="140dip"
            android:layout_height="40dip"
            android:layout_marginLeft="3dip"
            android:background="@drawable/button_background"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>
  • button_background.xml 按钮的背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <!-- 边框 -->
    <stroke
        android:width="0.5dip"
        android:color="#ff107048" />

    <!-- 指定边角 -->
    <corners android:radius="2dip" />

    <!-- 渐变色 -->
    <gradient
        android:centerColor="#ff107048"
        android:endColor="#ff085830"
        android:startColor="#ff109860" />

</shape>
  • LostProtectedActivity.java

package com.liuhao.mobilesafe.ui;

import com.liuhao.mobilesafe.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

public class LostProtectedActivity extends Activity {

    private static final String TAG = "LostProtectedActivity";
    private SharedPreferences sp;
    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        // 判读用户是否已经设置了密码
        if (isPwdSetup()) {
            Log.i(TAG, "设置了密码,弹出输入密码的对话框");
        } else {
            Log.i(TAG, "未设置密码,弹出设置密码对话框");
            showFirstEntryDialog();
        }
    }

    /**
     * 第一次进入程序时弹出的设置密码的对话框 自定义对话框样式
     */
    private void showFirstEntryDialog() {
        dialog = new Dialog(this, R.style.MyDialog);
        dialog.setContentView(R.layout.first_entry_dialog);// 设置要显示的内容
        dialog.show();
    }

    /**
     * 检查sharedpreference中是否有密码的设置
     *
     * @return
     */
    private boolean isPwdSetup() {
        String password = sp.getString("password", null);
        if (password == null) {
            return false;
        } else {
            if ("".equals(password)) {
                return false;
            } else {
                return true;
            }
        }
    }

}

最终效果:

时间: 2024-08-04 21:41:28

手机安全卫士06-手机防盗之自定义对话框的相关文章

【边做项目边学Android】手机安全卫士07-手机防盗之进入限制

上次写到在进入手机但·防盗界面时需要有密码限制,首先第一次进入时会弹出对话框提示用户设置密码:再次进入时会要求用户输入密码:这次来具体实现上述功能. 首次登录,设置密码 首先,我们的密码是保存在SharePreference中的"password"字段里的,在登录时后台需要校验该字段是否已经设置了密码,若未设置则弹出对话框让用户设置,否则要用户输入密码进入手机防盗界面: 校验是否设置了密码 @Override protected void onCreate(Bundle savedIn

【边做项目边学Android】手机安全卫士09-手机防盗界面设置向导1

本次主要做手机防盗界面的设置向导功能界面的设计. 需求: 当用户进入手机防盗界面时,判断用户是否已经进行过设置向导: 如果用户已经设置过手机防盗,则不再提示用户进入手机向导 若还没有设置,则提示用户进入设置向导界面. 具体实现: 1.当用户输入"手机防盗"密码正确时,进行判断用户是否进行过设置向导 /** * 判断用户是否进行过设置向导 * @return */ private boolean isSetup(){ return sp.getBoolean("isAlread

Android实例-手机安全卫士(二十三)-自定义抽象类及使用

一.目标. 将二十二节中通过滑动切换Activity界面效果的代码提取出来做成一个自定义抽象类,并定义抽象方法,便于其他类的调用.也就是其他Activity(如设置向导2.3.4)通过继承自定义的类,再通过实现其未实现的方法来快速实现滑动切换. 二.代码实现. 1.在程序包下新建一个类(取名SlideActivity),继承Activity,指定其类型为抽象类(public abstract class SlideActivity extends Activity).在自定义的抽象类代码中,定义

Android实例-手机安全卫士(四十)-自定义吐司(二)(布局样式、背景)

一. 自定义Toast的布局.背景等 二.代码实现 1.在res文件夹下的layout文件夹中新建布局文件(Android xml file,取名phone_add_toast),用于定义要显示的Toast的布局方式: 2.根据设计要求自定义的Toast布局为左右的水平线性布局,宽高均为包裹内容,左边为图片,右边为归属地信息文本(由于归属地信息会根据号码不同而改变,因此可为其设置id(tv_phone_add_toast)): (1)图片采用<ImageView>组件,通过android:sr

Android实例-手机安全卫士(二十一)-自定义Activity界面切换动画

一.目标. 实现两个Activity界面的动画切换效果. 二.代码实现. 1.在res文件夹下新建一个名为anim的文件夹. 2.在新建的anim文件夹中新建一个Android xml file文件(取名tran_out),根据动画要求选择根元素(本例为translate),用于实现Activity界面移出屏幕的动画效果. 3.在新建的xml文件中,根标签<translate>的属性中,当输入一个属性时会自动增加命名空间.属性android:fromXDelta表示从哪个X轴来,原点为屏幕左上

手机安全卫士------手机防盗页面之自定义对话框&amp;MD5加密

功能需求: 用户点击主页面上的"手机防盗"按钮时,判断用户是否设置过密码. 如果没有设置过,则弹出输入密码对话框 如果设置过了,则弹出设置密码对话框 用户的密码要进行MD5加密之后再存储在内存中 技术点: - 自定义对话框的使用 - MD5加密的实现方式 - SharedPreferences的读写操作 自定义对话框 1.在layout目录下创建一个布局文件,把自定义的对话框布局设置成功 具体代码实现如下 设置密码对话框的布局代码: <?xml version="1.0

Android实例-手机安全卫士(十)-自定义对话框

一.目标. 当点击“手机防盗”时弹出自定义的设置密码对话框.如果已经设置密码,则弹出自定义的输入密码对话框. 二.代码实现. 1.在layout文件夹下新建xml(取名setpwddialog.xml)文件用于设置自定义对话框的UI. 新建xml文件代码如下; 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.a

Android实例-手机安全卫士(十一)-自定义对话框点击事件处理

一.目标 在弹出的自定义对话框中,当点击“确认”时,保存密码,关闭对话框:当点击“取消”时关闭对话框.同时,如果不输入密码或输入密码错误则弹出相应的提示. 二.代码实现. 1.在主界面代码类(HomeActivity)中定义设置防盗密码对话框中的对应TextView.Button等成员变量,并在显示设置防盗密码对话框方法(showSetPwdDialog)中通过view.findViewById方法找到对应的组件: 2.为“取消”按钮设置取消操作的监听事件并进行处理.通过setOnClickLi

手机卫士-06

手机卫士-06 课1 高级工具中归属地查询的优化 实现电话号码的查询:eg 0759 把address中的data2数据 在LocationDao.java中继续添加代码,配合数据库和正则表达式进行查询并返回值 //判断当前的电话号码是手机还是座机:1.如果是手机电话号码1[3\5\7\8]9,2.电话区号0,还有110之类,使用switch来判断位数,以此对号码进行分类查询 需要使用正则表达式来匹配手机号码的规则 老师资料里有正则表达式的资料 手机电话号码1[3\5\7\8]9前三位的出现的情