前面,我们已经把手机防盗的界面搭建好了,所以现在我们要开始正式编码了,由于前面我已经详细述说了,怎么去进行手机防盗,所以现在只贴代码了,代码里有详尽的注释,项目耗时一天。好吧!我觉得我还是需要讲一下,也为自己重新整理一下思路,首先我们需要创建两个Dialog,用以进行我们的用户密码输入,因为是防盗软件,所以我们需要在软件第一次运行,就设置用户名和密码,那么怎么实现我们的软件,第一次运行就会弹出那个对话框呢?答案很简单,那就是在SharedPerferences中创建一个字段,因为SharedPerferences第一次创建,里面肯定会没有数据,所以我们可以利用这一点,通过访问SharedPerferences取得一个字段。代码如下
- /*得到SharedPreferences文件对象,如果没有则创建*/
- m_SharedPreferences = getSharedPreferences("data", MODE_WORLD_WRITEABLE);
- firstLogin = m_SharedPreferences.getBoolean("first", true);//取得first值
- /*如果软件是第一次启动则弹出设置密码对话框*/
- if (firstLogin) {//判断是否第一次进入
- setPasswordDialog();//软件第一次启动设置密码对话框
- return;
- }else {
- loginPasswordDialog();//软件再次启动输入密码对话框
- }
感觉这样很啰嗦了,贴上代码自己看吧,注释详尽:
- package com.yuxi.lostprevent;
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.telephony.TelephonyManager;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- /*定义两个对话框*/
- private AlertDialog setPasswordDialog;
- private AlertDialog loginPasswordDialog;
- private SharedPreferences m_SharedPreferences; //定义SharedPreferences存储数据
- /*定义控件*/
- private EditText et_SafeNumber;
- private Button btn_startSafe,btn_Modify;
- boolean firstLogin; //定义对话判断
- boolean isSafe; //定义防盗判断
- private TelephonyManager tm;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /*得到SharedPreferences文件对象,如果没有则创建*/
- m_SharedPreferences = getSharedPreferences("data", MODE_WORLD_WRITEABLE);
- firstLogin = m_SharedPreferences.getBoolean("first", true); //取得first值
- tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); //得到TelephonyManager对象,用以得到IMSI码
- /*如果软件是第一次启动则弹出设置密码对话框*/
- if (firstLogin) { //判断是否第一次进入
- setPasswordDialog(); //软件第一次启动设置密码对话框
- return;
- }else {
- loginPasswordDialog(); //软件再次启动输入密码对话框
- }
- }
- private void loginPasswordDialog() {
- // TODO Auto-generated method stub
- LayoutInflater inflater = getLayoutInflater(); //到LayoutInflater对象
- View view = inflater.inflate(R.layout.setandlogindialog, null); //得到setandlogindialog布局文件对象
- /*根据控件ID取得控件对象*/
- final EditText et_UserName = (EditText) view.findViewById(R.id.et_UserName);
- final EditText et_Password = (EditText) view.findViewById(R.id.et_Password);
- Button btn_Ok = (Button) view.findViewById(R.id.btn_Ok);
- Button btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
- /*设置按钮属性 */
- btn_Ok.setText("登录");
- btn_cancle.setText("取消");
- /*OK按钮的点击事件*/
- btn_Ok.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- /*取得输入框中的值*/
- String userName = et_UserName.getText().toString();
- String password = et_Password.getText().toString();
- /*取得m_SharedPreferences中存储的值*/
- String name = m_SharedPreferences.getString("userName", "");
- String pwd = m_SharedPreferences.getString("password", "");
- /*判断用户名和密码*/
- if (userName.trim().equals(name) && password.trim().equals(pwd)) {
- init();
- loginPasswordDialog.dismiss(); //销毁对话框
- }else {
- Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show();
- return;
- }
- }
- });
- /*cancle按钮的点击事件*/
- btn_cancle.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- finish(); //退出应用
- }
- });
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("请输入用户名和密码");
- builder.setIcon(R.drawable.ic_launcher);
- builder.setView(view);
- loginPasswordDialog = builder.create();
- loginPasswordDialog.show();
- }
- private void init()
- {
- setContentView(R.layout.activity_main);
- /*根据控件ID取得控件对象*/
- et_SafeNumber = (EditText) findViewById(R.id.safeNumber);
- btn_startSafe = (Button) findViewById(R.id.btn_startSafe);
- btn_Modify = (Button) findViewById(R.id.btn_modify);
- String number = m_SharedPreferences.getString("safenumber", "");
- et_SafeNumber.setText(number);
- isSafe = m_SharedPreferences.getBoolean("safestate", false);
- if (isSafe) {
- et_SafeNumber.setEnabled(false);
- btn_Modify.setEnabled(false);
- btn_startSafe.setText("关闭防盗");
- }
- btn_startSafe.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Editor editor = m_SharedPreferences.edit();
- if (isSafe) {
- /*将信息写入SharedPreferences中*/
- editor.putBoolean("safestate", false);
- editor.commit();
- btn_startSafe.setText("开始防盗");
- /*设置按钮为可见状态*/
- btn_Modify.setEnabled(true);
- et_SafeNumber.setEnabled(true);
- isSafe = false;
- }else {
- String numberStr = et_SafeNumber.getText().toString();
- if (numberStr.trim().equals("")) {
- Toast.makeText(MainActivity.this, "安全号码不能为空,请从新设置", Toast.LENGTH_LONG).show();
- return;
- }
- else {
- //IMSI信息
- String subScribeerID = tm.getSubscriberId();
- /*将信息写入SharedPreferences中*/
- editor.putString("safenumber", numberStr);
- isSafe = true;
- editor.putBoolean("safestate", isSafe);
- editor.putString("subscriberrid", subScribeerID);
- editor.commit();
- /*设置按钮为不可见状态*/
- btn_Modify.setEnabled(false);
- et_SafeNumber.setEnabled(false);
- btn_startSafe.setText("停止防盗"); //设置按钮显示文本属性
- }
- }
- }
- });
- btn_Modify.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- setPasswordDialog();
- }
- });
- }
- private void setPasswordDialog() {
- // TODO Auto-generated method stub
- LayoutInflater inflater = getLayoutInflater(); //到LayoutInflater对象
- View view = inflater.inflate(R.layout.setandlogindialog, null); //得到setandlogindialog布局文件对象
- /*根据控件ID取得控件对象*/
- final EditText et_UserName = (EditText) view.findViewById(R.id.et_UserName);
- final EditText et_Password = (EditText) view.findViewById(R.id.et_Password);
- Button btn_Ok = (Button) view.findViewById(R.id.btn_Ok);
- Button btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
- /*设置按钮属性*/
- btn_Ok.setText("设置");
- btn_cancle.setText("取消");
- /*OK按钮的点击事件*/
- btn_Ok.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- /*取得输入框中的值*/
- String userName = et_UserName.getText().toString();
- String password = et_Password.getText().toString();
- /*判断用户名和密码是否为空*/
- if (userName.trim().equals("") && password.trim().equals("")) {
- Toast.makeText(MainActivity.this, "请输入用户名和密码", Toast.LENGTH_LONG).show();
- }else {
- /*将信息写入SharedPreferences中*/
- Editor editor = m_SharedPreferences.edit();
- editor.putString("userName", userName);
- editor.putString("password", password);
- editor.putBoolean("first", false);
- editor.commit();
- /*销毁对话框,进入主界面*/
- setPasswordDialog.dismiss();
- init();
- }
- }
- });
- /*cancle按钮的点击事件*/
- btn_cancle.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- /*判断软件是否是第一次进入软件,第一次进入按取消按钮则退出软件,不是则销毁对话框*/
- if (firstLogin) {
- finish();
- }else {
- setPasswordDialog.dismiss();
- }
- }
- });
- AlertDialog.Builder builder = new AlertDialog.Builder(this); //创建一个对话
- builder.setTitle("请设置用户名和密码"); //设置标题
- builder.setIcon(R.drawable.ic_launcher); //设置ICon
- builder.setView(view); //设置显示的布局
- setPasswordDialog = builder.create(); //创建对话框
- setPasswordDialog.show(); //显示对话框
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
转载于:https://blog.51cto.com/shenlinyuxi/1194085