91.s1-手机防盗保护开启关闭

本文介绍了一个手机防盗功能的实现过程,包括如何通过监听防盗保护开关的状态来保存设置,并在不同页面间传递这些设置。此外,还展示了如何根据防盗功能的状态初始化安全电话号码。
通过监听防盗保护勾选的状态,分别保存不同的sMpr值,之后根据读取的值来判断是否需要设置相关的参数
布局页面Setup4_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ldw="http://schemas.android.com/apk/res/com.ldw.safe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    

    <TextView 
        style="@style/TitleStyle"
        android:text="4.恭喜您设置完成"
        />
    <CheckBox
        android:id="@+id/cb_protect"
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	android:text="防盗保护没有开启"
        />

     <LinearLayout 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:orientation="horizontal" > 
         
         <ImageView
            android:layout_width="wrap_content"
         	android:layout_height="wrap_content" 
         	android:src="@android:drawable/presence_invisible"/>
		<ImageView
            android:layout_width="wrap_content"
         	android:layout_height="wrap_content" 
         	android:src="@android:drawable/presence_invisible"/>
		<ImageView
            android:layout_width="wrap_content"
         	android:layout_height="wrap_content" 
         	android:src="@android:drawable/presence_invisible"/>
		<ImageView
            android:layout_width="wrap_content"
         	android:layout_height="wrap_content" 
         	android:src="@android:drawable/presence_online"/>		
      </LinearLayout>

     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="0dp" 
         android:layout_weight="1"
         >

         <ImageView
             android:id="@+id/imageView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_centerVertical="true"
             android:src="@drawable/phone" />

         <Button
             style="@style/NextStyle" 
             android:text="设置完成"
             />

         <Button
			style="@style/PreviousStyle"
             />

     </RelativeLayout>
    
</LinearLayout>    
监控放到保护的开启与关闭SettupActivity.java
package com.ldw.safe.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.ldw.safe.R;

/*
 * 手机防盗第四个设置向导页面
 */
public class Setup4Activity extends BaseSetupActivity {

	private CheckBox cbProtect;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup4);
        
        cbProtect = (CheckBox) findViewById(R.id.cb_protect);
        
        //根据保存的状态保存是否开启
       boolean protect = mPref.getBoolean("protect", false);
       if(protect){
    	   cbProtect.setText("防盗保护已经开启");
    	   cbProtect.setChecked(true);
       }else{
    	   cbProtect.setText("防盗保护没有开启");
    	   cbProtect.setChecked(false);
       }
       
        //监听checkBox,并保存checkbox状态
        cbProtect.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if(isChecked){
					cbProtect.setText("防盗保护已经开启");
					mPref.edit().putBoolean("protect", true).commit();
				}else{
					cbProtect.setText("防盗保护没有开启");
					mPref.edit().putBoolean("protect", false).commit();
				}
				
			}

        	
        });
	}
	
	//显示上一页,这个需要子类去实现,跳转的页面不是固定的
	public void showPrevious(){
		//页面跳转
		startActivity(new Intent(Setup4Activity.this, Setup3Activity.class));
		//销毁当前页面
		finish();
		//两个界面的切换动画
		overridePendingTransition(R.anim.tran_previous_in, R.anim.tran_previous_out);//进入动画和推出动画
	}
	
	//显示下一页,这个需要子类去实现,跳转的页面不是固定的
	public void showNext(){
		//页面跳转
		startActivity(new Intent(Setup4Activity.this, LostAndFind.class));
		//销毁当前页面
		finish();
		//更新mPref表示已经展示过设置向导,下次进来不展示更新向导
		mPref.edit().putBoolean("configed", true).commit();
		//两个界面的切换动画
		overridePendingTransition(R.anim.tran_in, R.anim.tran_out);//进入动画和推出动画
	}
	
}
同时在初始化进入的页面,根据防盗功能的开启和关闭初始化安全号码
LostAndFindActivity.java
package com.ldw.safe.Activity;

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.TextView;

import com.ldw.safe.R;

/*
 * 手机防盗页面
 */
public class LostAndFind extends Activity {

	private SharedPreferences mPref;
	private TextView tvSafePhone;
	private ImageView ivProtect;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        //判断是否是第一次登陆,第一次登陆会进入设置向导
        mPref = getSharedPreferences("config", MODE_PRIVATE);
       boolean configed =  mPref.getBoolean("configed", false);
       if(configed){
    	   //进入手机放到主页面
    	   setContentView(R.layout.activity_lostfind);
    	   //从mPref中获取到安全号码
    	   tvSafePhone = (TextView) findViewById(R.id.tv_safe_phone);
    	   String phone = mPref.getString("safe_phone", "");
    	   tvSafePhone.setText(phone);
    	   
    	   //从mPref中获取是否是开启保护的状态,来选择后面的图片是开启还是关闭
    	   ivProtect = (ImageView) findViewById(R.id.iv_protect);
    	   boolean protect = mPref.getBoolean("protect", false);
    	   if(protect){
    		   ivProtect.setImageResource(R.drawable.lock);
    	   }else{
    		   ivProtect.setImageResource(R.drawable.unlock);
    	   }
    	   
       }else{
    	   //跳转到设置向导页
    	   startActivity(new Intent(LostAndFind.this, Setup1Activity.class));
    	   finish();
       }
       
       
       
	}
	
    /*
     * 重新进入设置向导
     */
    public void reEnter(View v){
 	   startActivity(new Intent(LostAndFind.this, Setup1Activity.class));
 	   finish();
    }
}
  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值