Android手机安全卫士(二)---设置、自定义控件属性、md5加密、手机防盗

Android手机安全卫士(二)---设置、自定义控件属性、md5加密、手机防盗

①设置

设置中心主要用于设置手机是否自动获取更新,并要求使用SharedPreferences记住设置。




    

   

        

        

        
        
        
    
   


public class SettingActivity extends Activity {

	private TextView tv_desc;
	private CheckBox cb_status;
	private SharedPreferences sp;  
	private int flag;
	
	private Handler handler = new Handler() {  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 1:  
            	Log.v("--->>","Checked" );
            	String flag = "自动更新已经打开";
				tv_desc.setText(flag); 
				tv_desc.invalidate();
				break;
            case 0: 
            	Log.v("--->>","unChecked" );
            	String flag1 = "自动更新已经关闭"; 	
				tv_desc.setText(flag1);
				tv_desc.invalidate();
				break;
            }  
        }  
    };  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		cb_status = (CheckBox) findViewById(R.id.cb_status);
		tv_desc = (TextView) findViewById(R.id.tv_desc);
		SharedPreferences pref = getSharedPreferences("update", MODE_PRIVATE); 
	    flag=pref.getInt("update_flag", 0);;
		switch (flag) {  
        case 1:  
        	Log.v("--->>","Checked" );
        	cb_status.setChecked(true);
        	String flag1 = "自动更新已经打开";
			tv_desc.setText(flag1); 
			tv_desc.invalidate();
			break;
        case 0: 
        	Log.v("--->>","unChecked" );
        	cb_status.setChecked(false);
        	String flag2 = "自动更新已经关闭"; 	
			tv_desc.setText(flag2);
			tv_desc.invalidate();
			break;
        }  
		
		
		cb_status.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				// TODO 自动生成的方法存根
				new Thread(new Runnable() {  
					SharedPreferences.Editor editor = getSharedPreferences("update",MODE_PRIVATE).edit(); 
	                @Override  
	                public void run() { 

	                	Message message = new Message();
	                	if(cb_status.isChecked()){
	                		message.what = 1;  
	                		editor.putInt("update_flag", 1);
	                		editor.commit();  
	    				}else{
	    					message.what = 0;  
	    					editor.putInt("update_flag", 0);
	    					editor.commit();  
	    				}
	                    handler.sendMessage(message);  
	                }  
	            }).start();  
				
			}
			
		});
	}

}

效果如下图:
Android手机安全卫士(二)---设置、自定义控件属性、md5加密、手机防盗_第1张图片


②自定义控件属性

自定义组合控件的过程

1、自定义一个View 一般来说,继承相对布局,或者线性布局  ViewGroup。
2、实现父类的构造方法。一般来说,需要在构造方法里初始化自定义的布局文件。
3、根据一些需要或者需求,定义一些API方法。
4、根据需要,自定义控件的属性。
5、自定义命名空间,例如:
  xmlns:husheng="http://schemas.android.com/apk/res/包名"
  xmlns:husheng="http://schemas.android.com/apk/res/com.husheng.mobilesafe"
6、自定义我们的属性,在Res/values/attrs.xml



   
       
       
       
   



7、使用我们自定义的属性
 例如:
        husheng:title="设置自动更新"
        husheng:desc_on="设置自动更新已经开启"
        husheng:desc_off="设置自动更新已经关闭"

8、在我们自定义控件的带有两个参数的构造方法里AttributeSet attrs 取出我们的属性值,关联自定义布局文件对应的控件。

③md5加密

md5可以多次加密,加大解密难度。

public class MD5Utils {
	
	/**
	 * md5加密方法
	 * @param password
	 * @return
	 */
	public static String md5Password(String password) {

		try {
			// 得到一个信息摘要器
			MessageDigest digest = MessageDigest.getInstance("md5");
			byte[] result = digest.digest(password.getBytes());
			StringBuffer buffer = new StringBuffer();
			// 把没一个byte 做一个与运算 0xff;
			for (byte b : result) {
				// 与运算
				int number = b & 0xff;// 加盐
				String str = Integer.toHexString(number);
				// System.out.println(str);
				if (str.length() == 1) {
					buffer.append("0");
				}
				buffer.append(str);
			}

			// 标准的md5加密后的结果
			return buffer.toString();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}

	}

}

④手机防盗密码设置

通过弹出对话框来设置密码




    

    

   

    

        




    

    

    

    

        

public class HomeActivity extends Activity {
	
	private EditText et_setup_pwd;
	private EditText et_setup_confirm;
	private Button ok;
	private Button cancel;
	private AlertDialog dialog;
	private boolean flag;
	
	
	private GridView list_home;
	private MyAdapter adapter;
	private static String [] names = {
		"手机防盗","通讯卫士","软件管理",
		"进程管理","流量统计","手机杀毒",
		"缓存清理","高级工具","设置中心"
		
	};
	
	private static int[] ids = {
		R.drawable.safe,R.drawable.callmsgsafe,R.drawable.app,
		R.drawable.taskmanager,R.drawable.netmanager,R.drawable.trojan,
		R.drawable.sysoptimize,R.drawable.atools,R.drawable.settings
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		list_home = (GridView) findViewById(R.id.list_home);
		adapter = new MyAdapter();
		list_home.setAdapter(adapter);
		list_home.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				switch (position) {
				case 8://进入设置中心
					Intent intent = new Intent(HomeActivity.this,SettingActivity.class);
					startActivity(intent);
					
					break;
				case 0://进入手机防盗页面
					showLostFindDialog();
				default:
					break;
				}
				
			}

		});
	}
	
	private void showLostFindDialog() {
		// TODO 自动生成的方法存根
		if(isSetupPwd()){
			//已经设置密码了,弹出的是输入对话框
			showEnterDialog();
		}else{
			//没有设置密码,弹出的是设置密码对话框
			showSetupPwdDialog();
		}
		
	}
	
	private void showSetupPwdDialog() {
		
		// TODO 自动生成的方法存根
		
		AlertDialog.Builder builder = new Builder(HomeActivity.this);
		// 自定义一个布局文件
		View view = View.inflate(HomeActivity.this, R.layout.dialog_setup_password, null);
		et_setup_pwd = (EditText) view.findViewById(R.id.et_setup_pwd);
		et_setup_confirm = (EditText) view.findViewById(R.id.et_setup_confirm);
		ok = (Button) view.findViewById(R.id.ok);
		cancel = (Button) view.findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//把这个对话框取消掉
				dialog.dismiss();
			}
		});
		
		ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//  取出密码
				String password = et_setup_pwd.getText().toString().trim();
				String password_confirm = et_setup_confirm.getText().toString().trim();
				SharedPreferences.Editor editor = getSharedPreferences("password",MODE_PRIVATE).edit();
				//判断是否一致才去保存
				if(password.equals(password_confirm)&!(password.isEmpty())){
					//一致的话,就保存密码,把对话框消掉,还要进入手机防盗页面
					
					editor.putString("password", MD5Utils.md5Password(password));//保存加密后的
					editor.commit();
					dialog.dismiss();
					Toast.makeText(HomeActivity.this, "密码设置成功", 0).show();
					Intent intent = new Intent(HomeActivity.this,LostFindActivity.class);
					startActivity(intent);
				}else{
					if(password.isEmpty()){
						Toast.makeText(HomeActivity.this, "密码不能为空", 0).show();
					}else{
					editor.putString("password", null);//保存加密后的
					editor.commit();
					Toast.makeText(HomeActivity.this, "密码不一致", 0).show();
					return ;
					}
				}
				
				
				
			}
		});
		dialog = builder.create();
		dialog.setView(view, 0, 0, 0, 0);
		dialog.show();
		
	}

	private void showEnterDialog() {
		// TODO 自动生成的方法存根
		AlertDialog.Builder builder = new Builder(HomeActivity.this);
		// 自定义一个布局文件
		View view = View.inflate(HomeActivity.this, R.layout.dialog_enter_password, null);
		et_setup_pwd = (EditText) view.findViewById(R.id.et_setup_pwd);
		ok = (Button) view.findViewById(R.id.ok);
		cancel = (Button) view.findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//把这个对话框取消掉
				SharedPreferences pref = getSharedPreferences("password", MODE_PRIVATE); 
				//  取出密码
				String password = et_setup_pwd.getText().toString().trim();
				String savePassword = pref.getString("password", "");//取出加密后的
				if(password.isEmpty()){
					Toast.makeText(HomeActivity.this, "密码为空", 1).show();
					return;
				}
				
				if(MD5Utils.md5Password(password).equals(savePassword)){
					//输入的密码是我之前设置的密码
					//把对话框消掉,进入主页面;
					dialog.dismiss();
					showSetupPwdDialog();
					Toast.makeText(HomeActivity.this, "密码正确", 1).show();
					Intent intent = new Intent(HomeActivity.this,LostFindActivity.class);
					startActivity(intent);
					
				}else{
					Toast.makeText(HomeActivity.this, "密码错误", 1).show();
					et_setup_pwd.setText("");
					return;
				}
				
			}
		});
			ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SharedPreferences pref = getSharedPreferences("password", MODE_PRIVATE); 
				//  取出密码
				String password = et_setup_pwd.getText().toString().trim();
				String savePassword = pref.getString("password", "");//取出加密后的
				if(password.isEmpty()){
					Toast.makeText(HomeActivity.this, "密码为空", 1).show();
					return;
				}
				
				if(MD5Utils.md5Password(password).equals(savePassword)){
					//输入的密码是我之前设置的密码
					//把对话框消掉,进入主页面;
					dialog.dismiss();
					Toast.makeText(HomeActivity.this, "密码正确", 1).show();
					Intent intent = new Intent(HomeActivity.this,LostFindActivity.class);
					startActivity(intent);
					
				}else{
					Toast.makeText(HomeActivity.this, "密码错误", 1).show();
					et_setup_pwd.setText("");
					return;
				}
			}
		});
		dialog = builder.create();
		dialog.setView(view, 0, 0, 0, 0);
		dialog.show();
		
	}

	private boolean isSetupPwd() {
		// TODO 自动生成的方法存根
		SharedPreferences pref = getSharedPreferences("password", MODE_PRIVATE); 
		String name = pref.getString("password", "");
	    if(name == null || name.equals("")){
	    	Log.v("--->>", "没有设置密码");
			return false;
	    }else{
	    	Log.v("--->>", "已设置密码");
	    	return true;
	    	
	}}

	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return names.length;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = View.inflate(HomeActivity.this, R.layout.list_item_home, null);
			ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
			TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
			
			tv_item.setText(names[position]);
			iv_item.setImageResource(ids[position]);
			return view;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

	}

}

public class LostFindActivity extends Activity{

	private SharedPreferences sp;
	private boolean flag;
	private TextView reSet;
	
	private Handler handler = new Handler() {  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 1:  
            	Log.v("--->>","Checked" );
    			Intent intent = new Intent(LostFindActivity.this,Setup1Activity.class);    			
    			startActivity(intent);
    			//关闭当前页面
    			finish();
				break;
           
            }  
        }  
    };  
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		SharedPreferences pref = getSharedPreferences("config", MODE_PRIVATE); 
	    flag=pref.getBoolean("config", false);
	    if(flag){
			// 就在手机防盗页面
	    	Log.v("--->", "已经设置向导");
			setContentView(R.layout.activity_lost_find);
		}else{
			setContentView(R.layout.activity_lost_find);
			Log.v("--->", "还没有设置向导");//还没有做过设置向导
			new Thread(new Runnable() {  
                @Override  
                public void run() { 

                	Message message = new Message();
                	
                		message.what = 1;  
                		handler.sendMessage(message);  
                }  
            }).start();  
		}
	}
	public void reEnterSetup(View view){
		Intent intent = new Intent(this,Setup1Activity.class);
		startActivity(intent);
		//关闭当前页面
		finish();
	}
	
	

}



Android手机安全卫士(二)---设置、自定义控件属性、md5加密、手机防盗_第2张图片


Android手机安全卫士(二)---设置、自定义控件属性、md5加密、手机防盗_第3张图片


⑤手机设置向导

public class Setup1Activity extends Activity {
	
	private Button Setup1_next;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup1);
		Log.v("--->", "setup1");
		Setup1_next = (Button) findViewById(R.id.Setup1_next);
		Setup1_next.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Intent intent = new Intent(Setup1Activity.this,Setup2Activity.class);
				startActivity(intent);
				finish();
			}
			
		});
	}
	

}

public class Setup2Activity extends Activity {
	
	private Button Setup2_next;
	private Button Setup2_pre;
	private TextView sim_desc;
	private CheckBox sim_status;
	private SharedPreferences sp;  
	private int flag;
	private int button_flag;
	
	private Handler handler = new Handler() {  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 1:  
            	Log.v("--->>","Checked" );
            	String flag = "SIM卡已经绑定";
            	sim_desc.setText(flag); 
            	sim_desc.invalidate();
				break;
            case 0: 
            	Log.v("--->>","unChecked" );
            	String flag1 = "SIM卡没有绑定"; 	
            	sim_desc.setText(flag1);
            	sim_desc.invalidate();
				break;
            }  
        }  
    };  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup2);
		Log.v("--->", "setup2");
		
		sim_status = (CheckBox) findViewById(R.id.sim_status);
		sim_desc = (TextView) findViewById(R.id.sim_desc);
		SharedPreferences pref = getSharedPreferences("sim", MODE_PRIVATE); 
	    flag=pref.getInt("sim_flag", 0);
		switch (flag) {  
        case 1:  
        	Log.v("--->>","sim Checked" );
        	sim_status.setChecked(true);
        	String flag1 = "SIM卡已经绑定";
        	sim_desc.setText(flag1); 
        	sim_desc.invalidate();
			break;
        case 0: 
        	Log.v("--->>","sim unChecked" );
        	sim_status.setChecked(false);
        	Log.v("--->>","sim_status unChecked" );
        	String flag2 = "SIM卡没有绑定"; 	
        	sim_desc.setText(flag2);
        	Log.v("--->>","sim_desc unChecked" );
        	sim_desc.invalidate();
			break;
        }  
		/*****************************************************************/
		sim_status.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				// TODO 自动生成的方法存根
				new Thread(new Runnable() {  
					SharedPreferences.Editor editor = getSharedPreferences("sim",MODE_PRIVATE).edit(); 
	                @Override  
	                public void run() { 

	                	Message message = new Message();
	                	if(sim_status.isChecked()){
	                		message.what = 1;  
	                		editor.putInt("sim_flag", 1);
	                		editor.commit();  
	    				}else{
	    					message.what = 0;  
	    					editor.putInt("sim_flag", 0);
	    					editor.commit();  
	    				}
	                    handler.sendMessage(message);  
	                }  
	            }).start();  
				
			}
			
		});
		/***************************************************************/
		Setup2_next = (Button) findViewById(R.id.setup2_next);
		Setup2_next.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				SharedPreferences pref = getSharedPreferences("sim", MODE_PRIVATE); 
			    button_flag=pref.getInt("sim_flag", 0);
			    switch (button_flag) {  
		        case 1:  
		        	Intent intent = new Intent(Setup2Activity.this,Setup3Activity.class);
					startActivity(intent);
					finish();
					break;
		        case 0: 
		        	Intent intent2 = new Intent(Setup2Activity.this,HomeActivity.class);
					startActivity(intent2);
					finish();
					break;
		        }  
				
			}
			
		});
		/****************************************************************/
		Setup2_pre = (Button) findViewById(R.id.setup2_pre);
		Setup2_pre.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Intent intent = new Intent(Setup2Activity.this,Setup1Activity.class);
				startActivity(intent);
				finish();
			}
			
		});
		/****************************************************************/
	}
	
	
	


}

public class Setup3Activity extends Activity {
	
	private Button Setup3_next;
	private Button Setup3_pre;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup3);
	
	
	/***************************************************************/
	Setup3_next = (Button) findViewById(R.id.setup3_next);
	Setup3_next.setOnClickListener(new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			Log.v("--->", "go to setup4");
			Intent intent = new Intent(Setup3Activity.this,Setup4Activity.class);
			startActivity(intent);
			finish();
		}
		
	});
	/****************************************************************/
	Setup3_pre = (Button) findViewById(R.id.setup3_pre);
	Setup3_pre.setOnClickListener(new OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			Log.v("--->", "go to setup2");
			Intent intent = new Intent(Setup3Activity.this,Setup2Activity.class);
			startActivity(intent);
			finish();
		}
		
	});
	/****************************************************************/
	}
}
public class Setup4Activity extends Activity {
	
	private Button Setup4_next;
	private Button Setup4_pre;
	private TextView safe_desc;
	private CheckBox safe_status;
	private SharedPreferences sp;  
	private int flag;
	
	
	private Handler handler = new Handler() {  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 1:  
            	Log.v("--->>","Checked" );
            	String flag = "防盗保护已经开启";
            	safe_desc.setText(flag); 
            	safe_desc.invalidate();
				break;
            case 0: 
            	Log.v("--->>","unChecked" );
            	String flag1 = "防盗保护没有开启"; 	
            	safe_desc.setText(flag1);
            	safe_desc.invalidate();
				break;
            }  
        }  
    };  
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup4);
		Log.v("--->>","setup4" );
//		sp = getSharedPreferences("config", MODE_PRIVATE);
		safe_status = (CheckBox) findViewById(R.id.safe_status);
		safe_desc = (TextView) findViewById(R.id.safe_desc);
		SharedPreferences pref = getSharedPreferences("safe", MODE_PRIVATE); 
	    flag=pref.getInt("safe_flag", 0);
		switch (flag) {  
        case 1:  
        	Log.v("--->>","safe Checked" );
        	safe_status.setChecked(true);
        	String flag1 = "防盗保护已经开启";
        	safe_desc.setText(flag1); 
        	safe_desc.invalidate();
			break;
        case 0: 
        	Log.v("--->>","safe unChecked" );
        	safe_status.setChecked(false);
        	Log.v("--->>","safe_status unChecked" );
        	String flag2 = "防盗保护没有开启"; 	
        	safe_desc.setText(flag2);
        	Log.v("--->>","safe_desc unChecked" );
        	safe_desc.invalidate();
			break;
        }  
		/*****************************************************************/
		safe_status.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				// TODO 自动生成的方法存根
				new Thread(new Runnable() {  
					SharedPreferences.Editor editor = getSharedPreferences("safe",MODE_PRIVATE).edit(); 
	                @Override  
	                public void run() { 

	                	Message message = new Message();
	                	if(safe_status.isChecked()){
	                		message.what = 1;  
	                		editor.putInt("safe_flag", 1);
	                		editor.commit();  
	    				}else{
	    					message.what = 0;  
	    					editor.putInt("safe_flag", 0);
	    					editor.commit();  
	    				}
	                    handler.sendMessage(message);  
	                }  
	            }).start();  
				
			}
			
		});
		/***************************************************************/
		Setup4_next = (Button) findViewById(R.id.setup4_next);
		Setup4_next.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
		
					SharedPreferences.Editor editor = getSharedPreferences("config",MODE_PRIVATE).edit(); 
	                editor.putBoolean("config", true);
	                editor.commit();  
	    			
				Intent intent = new Intent(Setup4Activity.this,HomeActivity.class);
				startActivity(intent);
				finish();
			}
			
		});
		/****************************************************************/
		Setup4_pre = (Button) findViewById(R.id.setup4_pre);
		Setup4_pre.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Intent intent = new Intent(Setup4Activity.this,Setup3Activity.class);
				startActivity(intent);
				finish();
			}
			
		});
		/****************************************************************/
	}
	
	
	


	}

Android手机安全卫士(二)---设置、自定义控件属性、md5加密、手机防盗_第4张图片

Android手机安全卫士(二)---设置、自定义控件属性、md5加密、手机防盗_第5张图片


你可能感兴趣的:(未分类)