首页 > 代码库 > 24手机防盗的完成

24手机防盗的完成

我们需要在LostFindActivity这个类中,展示第三个设置向导页面传过来的安全号码,第四个页面如果勾选上了,则将防盗保护是否开启的图片换掉,最终结果如下图。




首先看最后一个设置向导页面:


package com.ustc.mobilemanager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Setup4Activity extends BaseSetupActivity {

	private SharedPreferences sp;
	
	private CheckBox cb_protecting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_setup4);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		cb_protecting = (CheckBox) findViewById(R.id.cb_protecting);
		
		boolean protecting = sp.getBoolean("protecting", false);
		
		if (protecting) {
			//手机防盗已经开启
			cb_protecting.setText("手机防盗已经开启");
			cb_protecting.setChecked(true);
		}else {
			//手机防盗没有开启
			cb_protecting.setText("手机防盗没有开启");
			cb_protecting.setChecked(false);
		}
		cb_protecting.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

				if (isChecked) {
					cb_protecting.setText("手机防盗已经开启");
				}else {
					cb_protecting.setText("手机防盗没有开启");
				}
				
				//保存选择的状态
				Editor editor = sp.edit();
				editor.putBoolean("protecting", isChecked);
				editor.commit();
			}
		});
		
		
	}

	@Override
	public void showBack() {
		Intent intent = new Intent(this, Setup3Activity.class);
		startActivity(intent);
		finish();
		// 要求finish()或者startActivity(intent)方面后面执行
		overridePendingTransition(R.anim.tran_pre_in, R.anim.tran_pre_out);

	}

	
	public void ok(View view) {
		Editor edit = sp.edit();
		edit.putBoolean("configed", true);
		edit.commit();
		Intent intent = new Intent(this, LostFindActivity.class);
		startActivity(intent);
		finish();
		// 要求finish()或者startActivity(intent)方面后面执行
		overridePendingTransition(R.anim.tran_in, R.anim.tran_out);

	}

	@Override
	public void showNext() {
		
		
	}
}

根据CheckBox的状态设置响应的字体。

package com.ustc.mobilemanager;

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

public class LostFindActivity extends Activity {

	private SharedPreferences sp;
	
	private ImageView iv_protecting;
	
	private TextView tv_safenumber;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		// 判断一下,是否做过设置向导,如果没有做过,就跳转到设置向导页面去设置,否则就留在当前的页面
		boolean configed = sp.getBoolean("configed", false);
		if (configed) {
			// 就在手机防盗页面
			setContentView(R.layout.activity_lost_find);
			
			tv_safenumber = (TextView) findViewById(R.id.tv_safenumber);
			iv_protecting  = (ImageView) findViewById(R.id.iv_protecting);
			
			//设置安全号码
			String safenumber = sp.getString("safenumber", "");
			if (safenumber != null) {
				tv_safenumber.setText(safenumber);
			}
			
			//设置防盗保护的状态
			boolean protecting = sp.getBoolean("protecting", false);
			if (protecting) {
				iv_protecting.setImageResource(R.drawable.strongbox_app_lock_ic_locked);
			}else {
				iv_protecting.setImageResource(R.drawable.strongbox_app_lock_ic_unlock);
			}
		} else {
			// 还没有做过设置向导
			Intent intent = new Intent(LostFindActivity.this,
					Setup1Activity.class);
			startActivity(intent);
			finish();
			// 要求finish()或者startActivity(intent)方面后面执行
			overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
		}

	}

	public void back(View view) {
		Intent intent = new Intent(LostFindActivity.this, HomeActivity.class);
		startActivity(intent);
		finish();
		//要求finish()或者startActivity(intent)方面后面执行
		overridePendingTransition(R.anim.tran_pre_in, R.anim.tran_pre_out);
	}

	/**
	 * 重新进入防盗设置页面(TextView的点击事件)
	 * 
	 * @param view
	 */
	public void reEnterSetup(View view) {
		Intent intent = new Intent(LostFindActivity.this, Setup1Activity.class);
		startActivity(intent);
		finish();
		// 要求finish()或者startActivity(intent)方面后面执行
		overridePendingTransition(R.anim.tran_in, R.anim.tran_out);

	}
}

拿到安全号码和设置向导页面4设置的状态。主要是SharedPreferences的操作。


一个错误:

空指针异常,LostFindActivity类中,忘了finfviewbyid就去设置TextView的值,粗心!!!





24手机防盗的完成