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毫秒。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值