手机卫士知识点之------手机防盗功能1

package cn.itcast.mobilesafe.ui;

import cn.itcast.mobilesafe.R;
import cn.itcast.mobilesafe.adapter.MainUIAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
	private static final String TAG = "MainActivity";
	private GridView gv_main;
	private MainUIAdapter adapter;
	// 用来持久化一些配置信息
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainscreen);
		sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
		gv_main = (GridView) this.findViewById(R.id.gv_main);
		//得到MainUIAdapter的adapter
		adapter = new MainUIAdapter(this);
		//设置adapter.
		gv_main.setAdapter(adapter);
		//设置每一项的点击事件.
		gv_main.setOnItemClickListener(this);

		gv_main.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> parent, final View view,
					int position, long id) {
				if (position == 0) {
					//得到AlertDialog.Builder
					AlertDialog.Builder buider = new Builder(MainActivity.this);
					buider.setTitle("设置");
					buider.setMessage("请输入要更改的名称");
					final EditText et = new EditText(MainActivity.this);
					et.setHint("请输入文本");
					buider.setView(et);
					//
					buider.setPositiveButton("确定", new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							//得到输入的迷惑名字
							String name = et.getText().toString().trim();
							if ("".equals(name)) {
								Toast.makeText(getApplicationContext(),
										"内容不能为空", 0).show();
								return;
							} else {
								Editor editor = sp.edit();
								editor.putString("lost_name", name);
								// 完成数据的提交
								editor.commit();
								TextView tv  = (TextView) view.findViewById(R.id.tv_main_name);
								//把名字更改,
								tv.setText(name);
							}

						}
					});
					buider.setNegativeButton("取消", new OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
						}
					});

					buider.create().show();
				}
				return false;
			}
		});
	}

	/**
	 * 当gridview的条目被点击的时候 对应的回调 parent : girdview 
	 * view : 当前被点击的条目 Linearlayout
	 * position : 点击条目对应的位置
	 *  id : 代表的行号
	 */

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Log.i(TAG, "点击的位置" + position);
		switch (position) {
		case 0: // 手机防盗
			Log.i(TAG, "进入手机防盗");
			//点击事件 ,进入LostProtectedActivity.
			Intent lostIntent = new Intent(MainActivity.this, LostProtectedActivity.class);
			startActivity(lostIntent);
			break;
		case 1: // 通讯卫士
			Log.i(TAG, "进入通讯防盗");
			Intent callsmsIntent = new Intent(MainActivity.this, CallSmsActivity.class);
			startActivity(callsmsIntent);
			break;
		case 2: // 程序管理
			Intent appmanagerIntent = new Intent(MainActivity.this, AppManagerActivity.class);
			startActivity(appmanagerIntent);
			break;
		case 7: // 高级工具
			Log.i(TAG, "进入高级工具");
			Intent atoolsIntent = new Intent(MainActivity.this, AtoolsActivity.class);
			startActivity(atoolsIntent);
			break;
		}

	}

}
<pre class="java" name="code">package cn.itcast.mobilesafe.ui;

import cn.itcast.mobilesafe.R;
import cn.itcast.mobilesafe.util.MD5Encoder;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class LostProtectedActivity extends Activity implements OnClickListener {
	private static final String TAG = "LostProtectedActivity";
	private SharedPreferences sp;
	private Dialog dialog;
	private EditText et_pwd;
	private EditText et_pwd_confirm;
	private TextView tv_lost_protected_number;
	private TextView tv_reentry_setup_guide;
	private CheckBox cb_isprotecting;
	//这里用到了自定义对话框dialog.
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//得到sp.初始化.
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		// 在进入后就立即进行判断,判断用户是否已经设置了密码 
		//如果设置了密码,也就是sp里面有值.就显示正常登陆的对话框
		if(isPWDSteup()){
			Log.i(TAG,"设置了密码,正常登陆的对话框");
			
			showNormalEntryDialog();
		}else{
			//如果没有设置密码,就显示登陆注册的对话框.
			Log.i(TAG,"没有设置密码,显示第一次对话框");
			showFirstEntryDialog();
		}
		
	}

	/**
	 * 正常登陆的对话框
	 */
	private void showNormalEntryDialog() {
		dialog = new Dialog(this, R.style.MyDialog);
		//点后退键不能取消.
		dialog.setCancelable(false);
		//dialog.setContentView(R.layout.first_entry_dialog);
		//inflate方法二.内部实现一样.
		View view = View.inflate(this, R.layout.normal_entry_dialog, null);
		et_pwd = (EditText) view.findViewById(R.id.et_normal_entry_pwd);
		Button bt_normal_ok = (Button) view.findViewById(R.id.bt_normal_dialog_ok);
		Button bt_normal_cancle =  (Button) view.findViewById(R.id.bt_normal_dialog_cancle);
		bt_normal_ok.setOnClickListener(this);
		bt_normal_cancle.setOnClickListener(this);
		dialog.setContentView(view);
		dialog.show();
		
	}


	/**
	 * 第一次进入程序时候的对话框
	 * android.jar  sdk\platforms\android-10.所有的class和资源文件.xml
	 * 反编译android.jar.  apktool可以反编译jar和class文件.所有的样式和主题都在value下.
	 */
	//自定义对话框.更改系统默认对话框的样式.
//	  <style name="MyDialog" parent="@android:style/Theme.Dialog">引用父类
//     更改父类的背景 <item name="android:windowBackground">@drawable/title_background</item>
//     添加的item <item name="android:windowNoTitle">true</item>
//  </style>
//	android:layout_marginLeft="5dip" 控件与控件之间
//	padding_top   ... 控件里面的东西 距离控件边框的距离
	//layout_margaintop ... 控件与控件之间的距离
	//图片可以使选择器
	private void showFirstEntryDialog() {
		//R.style.MyDialog.
		dialog = new Dialog(this, R.style.MyDialog);
		dialog.setCancelable(false);
		//这种办法可以,但是我们没法用界面里面的控件.
		//dialog.setContentView(R.layout.first_entry_dialog);
		View view = View.inflate(this, R.layout.first_entry_dialog, null);
		et_pwd = (EditText) view.findViewById(R.id.et_first_entry_pwd);
		et_pwd_confirm = (EditText) view.findViewById(R.id.et_first_entry_pwd_confirm);
		Button bt_ok = (Button) view.findViewById(R.id.bt_first_dialog_ok);
		Button bt_cancle =  (Button) view.findViewById(R.id.bt_first_dialog_cancle);
		bt_ok.setOnClickListener(this);
		bt_cancle.setOnClickListener(this);
		
		dialog.setContentView(view);
		dialog.show();
	}


	/**
	 * 检查sharedpreference里面是否有密码的设置
	 * @return
	 */
	private boolean isPWDSteup(){
		//如果里面有数据,发挥为真,else返回为假.
	   String password =	sp.getString("password",null);
	   if(password==null){
		   return false;
	   }else{
		   if("".equals(password)){
			   return false;
		   }else{
			   return true;
		   }
	   }
	}


	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_first_dialog_cancle:
			dialog.dismiss();
			finish();
			break;
			//得到pwd.
		case R.id.bt_first_dialog_ok:
			String pwd = et_pwd.getText().toString().trim();
			String pwd_confirm = et_pwd_confirm.getText().toString().trim();
			if("".equals(pwd)||"".equals(pwd_confirm)){
				Toast.makeText(getApplicationContext(), "密码不能为空", 0).show();
				return;
			}else{
				
				if(pwd.equals(pwd_confirm)){
					//得到editor
					Editor editor = sp.edit();
					//写入加密后的pwd.
					editor.putString("password", MD5Encoder.encode(pwd));
					editor.commit();
				}
				else{
					Toast.makeText(getApplicationContext(), "两次密码不同", 0).show();
					return;
				}
			}
			dialog.dismiss();
			break;
		case R.id.bt_normal_dialog_cancle:
			dialog.dismiss();
			finish();
			break;
			
			//普通登陆的时候.
		case R.id.bt_normal_dialog_ok:
			String password = et_pwd.getText().toString().trim();
			if("".equals(password)){
				Toast.makeText(getApplicationContext(), "请输入密码", 0).show();
				return;
			}else{
				//realpwd为得到sp里面的密码.  
				//MD5Encoder.encode(password)为把新输入进的密码.
				String realpwd = sp.getString("password", "");
				if (realpwd.equals(MD5Encoder.encode(password))){
					
					if(isSteup()){ // true 
						//在正常登陆的时候,也就是输入密码正确的时候.
						Log.i(TAG,"加载手机防盗主界面");
						//md5加密,是不可逆的.
						setContentView(R.layout.lost_protected);
						tv_lost_protected_number = (TextView) this.findViewById(R.id.tv_lost_protected_number);
						tv_reentry_setup_guide = (TextView )this.findViewById(R.id.tv_reentry_setup_guide);
						cb_isprotecting = (CheckBox )this.findViewById(R.id.cb_isprotecting);
						// 初始化这些控件 
						String number = sp.getString("safenumber", "");
						tv_lost_protected_number.setText("安全手机号码为:"+number);
						//重新进入设置向导的点击事件
						tv_reentry_setup_guide.setOnClickListener(this);
						// 初始化checkbox的状态
						boolean isprotecting = sp.getBoolean("isprotecting", false);
						if(isprotecting){
							cb_isprotecting.setText("手机防盗保护中");
							cb_isprotecting.setChecked(true);
						}
		
			cb_isprotecting.setOnCheckedChangeListener(new OnCheckedChangeListener() {
							
							public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
								if(isChecked){
									cb_isprotecting.setText("手机防盗保护中");
									Editor editor = sp.edit();
									editor.putBoolean("isprotecting", true);
									editor.commit();
								}else {
									cb_isprotecting.setText("没有开启防盗保护");
									Editor editor = sp.edit();
									editor.putBoolean("isprotecting", false);
									editor.commit();
								}
								
							}
						});
						
						
					}else{
						Log.i(TAG,"激活设置向导界面");
						finish();
						//激活设置向导界面.
						Intent intent = new Intent(LostProtectedActivity.this,SetupGudie1Activity.class);
						startActivity(intent);
					}
					
					
				}else{
					Toast.makeText(getApplicationContext(), "密码错误", 0).show();
					return;
				}
			}
			dialog.dismiss();
			break;
		case R.id.tv_reentry_setup_guide:
			finish();
			Intent intent = new Intent(LostProtectedActivity.this,SetupGudie1Activity.class);
			startActivity(intent);
		}
		
	}
	/**
	 * 判断用户是否进行过设置向导
	 * @return
	 */
	//判断是否设置过设置向导,或者判断是否保存过密码,都是这种类型的boolean.
	private boolean isSteup(){
		return  sp.getBoolean("issteupalready", false);
	}
	
}

 
 

你可能感兴趣的:(手机卫士知识点之------手机防盗功能1)