11、手机防盗--向导代码实现

上一节实现了向导界面,这里主要实现对应Activity。

清单文件添加四个Activity:

        <activity android:name=".LostProtectStep1Activity" />
        <activity android:name=".LostProtectStep2Activity" />
        <activity android:name=".LostProtectStep3Activity" />
        <activity android:name=".LostProtectStep4Activity" />

向导界面一比较简单,只要实现“下一步”按钮的响应事件next切换到向导界面二即可:

package com.example.mobilesafe;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


/**
 * Created by sing on 13-12-26.
 * desc:
 */
public class LostProtectStep1Activity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lostprotected_step1);
    }
    //下一步按钮的处理过程,显示第二个向导页面
    public void next(android.view.View view) {
        Intent intent = new Intent(this, LostProtectStep2Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.leftshowrigthin, R.anim.leftshowrigthout);
    }
}

向导界面二除了实现“上一步”和“下一步”的响应事件:prev和next,还要处理开启或者关闭绑定sim卡的操作:

package com.example.mobilesafe;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.utils.util;

/**
 * Created by sing on 13-12-26.
 * desc:
 */
public class LostProtectStep2Activity extends Activity implements View.OnClickListener {

    private RelativeLayout rl_step2_bind;
    private ImageView iv_step2_bind_status;
    private SharedPreferences sp;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lostprotected_step2);

        iv_step2_bind_status = (ImageView)findViewById(R.id.iv_step2_bind_status);
        rl_step2_bind = (RelativeLayout)findViewById(R.id.rl_step2_bind);
        rl_step2_bind.setOnClickListener(this);

        sp = getSharedPreferences("config", MODE_PRIVATE);
        String sim = sp.getString("sim", "");
        if (sim.isEmpty()) {
            iv_step2_bind_status.setImageResource(R.drawable.switch_off_normal);
        }else {
            iv_step2_bind_status.setImageResource(R.drawable.switch_on_normal);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_step2_bind:
                String sim = sp.getString("sim", "");
                SharedPreferences.Editor editor = sp.edit();
                if (sim.isEmpty()) {
                    //如果之前未设置,则设置
                    editor.putString("sim", util.getSimSerial(this));
                    iv_step2_bind_status.setImageResource(R.drawable.switch_on_normal);
                }else{
                    //如果之前设置过,则设置为空
                    editor.putString("sim", "");
                    iv_step2_bind_status.setImageResource(R.drawable.switch_off_normal);
                }
                editor.commit();
                break;
        }
    }

    //上一步按钮的处理过程,显示第一个向导页面
    public void prev(android.view.View view) {
        Intent intent = new Intent(this, LostProtectStep1Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.rightshowleftin, R.anim.rightshowleftout);
    }

    //下一步按钮的处理过程,显示第三个向导页面
    public void next(android.view.View view) {
        Intent intent = new Intent(this, LostProtectStep3Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.leftshowrigthin, R.anim.leftshowrigthout);
    }
}
获取sim卡序列号的功能放到util里了:
    /**
     * 获取sim卡,需要权限:android.permission.READ_PHONE_STATE
     * @return
     */
    public static String getSimSerial(Context context) {
        TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getSimSerialNumber();
    }

清单文件需要添加权限:

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

向导界面三因为要处理选择联系人的功能,这一节暂不实现,只实现前后切换的效果:
package com.example.mobilesafe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Created by sing on 13-12-26.
 * desc:
 */
public class LostProtectStep3Activity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lostprotected_step3);
    }

    //上一步按钮的处理过程,显示第二个向导页面
    public void prev(android.view.View view) {
        Intent intent = new Intent(this, LostProtectStep2Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.rightshowleftin, R.anim.rightshowleftout);
    }

    //下一步按钮的处理过程,显示第四个向导页面
    public void next(android.view.View view) {
        Intent intent = new Intent(this, LostProtectStep4Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.leftshowrigthin, R.anim.leftshowrigthout);
    }
}

到向导页面四,整个设置步骤即将结束,如果用户没有勾选“防盗保护开启”,则弹出对话框让用户开启,如果用户仍不开启则直接进入防盗页面。

package com.example.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

/**
 * Created by sing on 13-12-26.
 * desc:
 */
public class LostProtectStep4Activity extends Activity {

    private SharedPreferences sp;

    private CheckBox cb_step4_protect;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lostprotected_step4);

        sp = getSharedPreferences("config", MODE_PRIVATE);
        cb_step4_protect = (CheckBox)findViewById(R.id.cb_step4_protect);
        cb_step4_protect.setChecked(sp.getBoolean("protecting", false));
        cb_step4_protect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("protecting", b?true:false);
                cb_step4_protect.setText(b?"防盗保护已经开启":"防盗保护没有开启");
            }
        });
    }

    //上一步按钮的处理过程,显示第三个向导页面
    public void prev(android.view.View view) {
        Intent intent = new Intent(this, LostProtectStep3Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.rightshowleftin, R.anim.rightshowleftout);
    }

    //下一步按钮的处理过程,显示防盗页面
    public void next(android.view.View view) {

        //到这一步认为用户已经进行过防盗设置了,下一次进入不再自动显示向导
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("lostset", true);
        editor.commit();

        //如果防盗保护未开启,给予提醒
        if (cb_step4_protect.isChecked()==false) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("温馨提示");
            builder.setMessage("手机防盗极大地保护您的手机安全,强烈建议开启!");
            builder.setPositiveButton("开启", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    cb_step4_protect.setChecked(true);
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //用户不勾选并且提示后也不开启防盗保护,则直接进入手机防盗页面
                    Intent intent = new Intent(LostProtectStep4Activity.this, LostProtectedActivity.class);
                    startActivity(intent);
                    finish();
                    overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
                }
            });

            builder.create().show();
        } else {
            //用户勾选并开启防盗保护,则直接进入手机防盗页面
            Intent intent = new Intent(this, LostProtectedActivity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
        }
    }
}

本节使用了页面切换效果:从右向左,从左到右,淡进淡出。

实现切换效果的参数意义:第一个参数是将要显示的页面的出现效果,第二个参数是将要消失的界面的消失效果。

分别介绍一下:

alpha_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="300"
    >
</alpha>
alpha_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<alpha  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="300"
    >
</alpha>
这个效果解释是:第二个页面从0到1慢慢显示出来,时长300毫秒,第一个页面从1到0慢慢消失,时长300毫秒。


leftshowrigthin.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:fromXDelta="100%p"
    android:toXDelta="0"
    android:duration="300"
    >
</translate>

leftshowrigthout.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:fromXDelta="0"
    android:toXDelta="-100%p"
    android:duration="300"
    >
</translate>
这个效果解释是:第二个页面移进的方式为从右侧(100)移动到左侧(0),时长300毫秒,第一个页面移出的方式从左侧(0)移走(-100),时长300毫秒。


rightshowleftin.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:fromXDelta="-100%p"
    android:toXDelta="0"
    android:duration="300"
    >
</translate>
rightshowleftout.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:fromXDelta="0"
    android:toXDelta="100%p"
    android:duration="300"
    >
</translate>
这个效果解释是:第二个页面移进的方式为从左侧(-100)移进(0),时长300毫秒,第一个页面移出的方式从左侧(0)移动到右侧(100),时长300毫秒。


你可能感兴趣的:(11、手机防盗--向导代码实现)