LostFindActivity.java
public class LostFindActivity extends Activity { private SharedPreferences mPrefs; private TextView tvSafePhone; private ImageView ivProtect; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPrefs = getSharedPreferences("config", MODE_PRIVATE); boolean configed = mPrefs.getBoolean("configed", false);// 判断是否进入过设置向导 if (configed) { setContentView(R.layout.activity_lost_find); // 根据sp更新安全号码 tvSafePhone = (TextView) findViewById(R.id.tv_safe_phone); String phone = mPrefs.getString("safe_phone", ""); tvSafePhone.setText(phone); // 根据sp更新保护锁 ivProtect = (ImageView) findViewById(R.id.iv_protect); boolean protect = mPrefs.getBoolean("protect", false); if (protect) { ivProtect.setImageResource(R.drawable.lock); } else { ivProtect.setImageResource(R.drawable.unlock); } } else { // 跳转设置向导页 startActivity(new Intent(this, Setup1Activity.class)); finish(); } } /** * 重新进入设置向导 * * @param view */ public void reEnter(View view) { startActivity(new Intent(this, Setup1Activity.class)); finish(); }}
activity_lost_find.xml
shape_selector.xml
gradient_box.xml
//矩形
下边是设置向导:
BaseSetupActivity.java
//不需要在清单文件中注册,因为不需要界面展示 public abstract class BaseSetupActivity extends Activity { private GestureDetector mDectector; public SharedPreferences mPref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPref = getSharedPreferences("config", MODE_PRIVATE); // 手势识别器 mDectector = new GestureDetector(this, new SimpleOnGestureListener() { /** * 监听手势滑动事件 e1表示滑动的起点,e2表示滑动终点 velocityX表示水平速度 velocityY表示垂直速度 */ @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 判断纵向滑动幅度是否过大, 过大的话不允许切换界面 if (Math.abs(e2.getRawY() - e1.getRawY()) > 100) { Toast.makeText(BaseSetupActivity.this, "不能这样划哦!", Toast.LENGTH_SHORT).show(); return true; } // 判断滑动是否过慢 if (Math.abs(velocityX) < 100) { Toast.makeText(BaseSetupActivity.this, "滑动的太慢了!", Toast.LENGTH_SHORT).show(); return true; } // 向右划,上一页 if (e2.getRawX() - e1.getRawX() > 200) { showPreviousPage(); return true; } // 向左划, 下一页 if (e1.getRawX() - e2.getRawX() > 200) { showNextPage(); return true; } return super.onFling(e1, e2, velocityX, velocityY); } }); } /** * 展示下一页, 子类必须实现 */ public abstract void showNextPage(); /** * 展示上一页, 子类必须实现 */ public abstract void showPreviousPage();//手势识别和点击按钮都会进入这里 // 点击下一页按钮 public void next(View view) { showNextPage(); } // 点击上一页按钮 public void previous(View view) { showPreviousPage(); } @Override public boolean onTouchEvent(MotionEvent event) { mDectector.onTouchEvent(event);// 委托手势识别器处理触摸事件 return super.onTouchEvent(event); }}
Setup1Activity.java
public class Setup1Activity extends BaseSetupActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setup1); } @Override public void showNextPage() { startActivity(new Intent(this, Setup2Activity.class)); finish(); // 两个界面切换的动画 overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画 } @Override public void showPreviousPage() { }}
activity_setup1.xml
styles.xml
btn_green_selector.xml //状态选择器(selector)设置给view,点击会有相应的效果
- //默认
.9.png(9patch)图片的制作:
通过黑色边线来描述图片的拉伸情况和填充文字的方式
上边线-水平拉伸;左边线-垂直拉伸;右边线-垂直填充区域;下边线-水平填充区域
Setup2Activity.java
public class Setup2Activity extends BaseSetupActivity { private SettingItemView sivSim; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setup2); sivSim = (SettingItemView) findViewById(R.id.siv_sim); String sim = mPref.getString("sim", null); if (!TextUtils.isEmpty(sim)) { sivSim.setChecked(true); } else { sivSim.setChecked(false); } sivSim.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (sivSim.isChecked()) { sivSim.setChecked(false); mPref.edit().remove("sim").commit();// 删除已绑定的sim卡 } else { sivSim.setChecked(true); // 选中的话,保存sim卡信息 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); String simSerialNumber = tm.getSimSerialNumber();// 获取sim卡序列号 System.out.println("sim卡序列号:" + simSerialNumber); mPref.edit().putString("sim", simSerialNumber).commit();// 将sim卡序列号保存在sp中 } } }); } @Override public void showNextPage() { // 如果sim卡没有绑定,就不允许进入下一个页面 String sim = mPref.getString("sim", null); if (TextUtils.isEmpty(sim)) { ToastUtils.showToast(this, "必须绑定sim卡!"); return; } startActivity(new Intent(this, Setup3Activity.class)); finish(); // 两个界面切换的动画 overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画 } @Override public void showPreviousPage() { startActivity(new Intent(this, Setup1Activity.class)); finish(); // 两个界面切换的动画 overridePendingTransition(R.anim.trans_previous_in, R.anim.trans_previous_out);// 进入动画和退出动画 }}
BootCompleteReceiver.java
public class BootCompleteReceiver extends BroadcastReceiver { // 监听手机开机启动的广播 @Override public void onReceive(Context context, Intent intent) { SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); boolean protect = sp.getBoolean("protect", false); // 只有在防盗保护开启的前提下才进行sim卡判断 if (protect) { String sim = sp.getString("sim", null);// 获取绑定的sim卡 if (!TextUtils.isEmpty(sim)) { // 获取当前手机的sim卡 TelephonyManager tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); String currentSim = tm.getSimSerialNumber() + "111";// 拿到当前手机的sim卡 if (sim.equals(currentSim)) { System.out.println("手机安全"); } else { System.out.println("sim卡已经变化, 发送报警短信!!!"); String phone = sp.getString("safe_phone", "");// 读取安全号码 // 发送短信给安全号码 SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phone, null, "sim card changed!", null, null); } } } } }
我们开启5554和5556的模拟器,给5554安全号码设置为5556,我们给5554的sim卡序列号变化,那么开机重启5554,开机启动的广播就会 检测到sim卡变更,发送短信给5556,5556就会接收到5554发送来的短信。
SmsReceiver .java
//拦截短信public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //pdu为承载着一条短信的所有短信。一条短信为140个英文字符长度,在这个长度范围内,即需一个pdu即可。超出这个范围,即要分割成多个pdu数组。 Object[] objects = (Object[]) intent.getExtras().get("pdus"); for (Object object : objects) { // 短信最多140字节, // 超出的话,会分为多条短信发送,所以是一个数组,因为我们的短信指令很短,所以for循环只执行一次 SmsMessage message = SmsMessage.createFromPdu((byte[]) object); String originatingAddress = message.getOriginatingAddress();// 短信来源号码 String messageBody = message.getMessageBody();// 短信内容 System.out.println(originatingAddress + ":" + messageBody); if ("#*alarm*#".equals(messageBody)) { // 播放报警音乐, 即使手机调为静音,也能播放音乐, 因为使用的是媒体声音的通道,和铃声无关 MediaPlayer player = MediaPlayer.create(context, R.raw.ylzs); player.setVolume(1f, 1f); player.setLooping(true); player.start(); abortBroadcast();// 中断短信的传递, 从而系统短信app就收不到内容了 } else if ("#*location*#".equals(messageBody)) { // 获取经纬度坐标 context.startService(new Intent(context, LocationService.class));// 开启定位服务 SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); String location = sp.getString("location", "getting location..."); System.out.println("location:" + location); abortBroadcast();// 中断短信的传递, 从而系统短信app就收不到内容了 } else if ("#*wipedata*#".equals(messageBody)) { System.out.println("远程清除数据"); abortBroadcast(); } else if ("#*lockscreen*#".equals(messageBody)) { System.out.println("远程锁屏"); abortBroadcast(); } } }}
我们在5556的手机上发送短信,5554的手机就会播放R.raw.ylzs的音乐。
LocationService.java
/* * 获取经纬度坐标的service */public class LocationService extends Service { private LocationManager lm; private MyLocationListener listener; private SharedPreferences mPref; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mPref = getSharedPreferences("config", MODE_PRIVATE); lm = (LocationManager) getSystemService(LOCATION_SERVICE); // ListallProviders = lm.getAllProviders();// 获取所有位置提供者 // System.out.println(allProviders); Criteria criteria = new Criteria(); criteria.setCostAllowed(true);// 是否允许付费,比如使用3g网络定位 criteria.setAccuracy(Criteria.ACCURACY_FINE); String bestProvider = lm.getBestProvider(criteria, true);// 获取最佳位置提供者 listener = new MyLocationListener(); lm.requestLocationUpdates(bestProvider, 0, 0, listener);// 参1表示位置提供者,参2表示最短更新时间,参3表示最短更新距离 } class MyLocationListener implements LocationListener { // 位置发生变化 @Override public void onLocationChanged(Location location) { System.out.println("get location!"); // 将获取的经纬度保存在sp中 mPref.edit().putString("location", "j:" + location.getLongitude() + "; w:" + location.getLatitude()) .commit(); stopSelf();// 停掉service,这样节电 } // 位置提供者状态发生变化 @Override public void onStatusChanged(String provider, int status, Bundle extras) { System.out.println("onStatusChanged"); } // 用户打开gps @Override public void onProviderEnabled(String provider) { System.out.println("onProviderEnabled"); } // 用户关闭gps @Override public void onProviderDisabled(String provider) { System.out.println("onProviderDisabled"); } } @Override public void onDestroy() { super.onDestroy(); lm.removeUpdates(listener);// 当activity销毁时,停止更新位置, 节省电量 }}
trans_in.xml
trans_out.xml
trans_previous_in.xml
trans_previous_out.xml
activity_setup2.xml
Setup3Activity.java
public class Setup3Activity extends BaseSetupActivity { private EditText etPhone; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setup3); etPhone = (EditText) findViewById(R.id.et_phone); String phone = mPref.getString("safe_phone", ""); etPhone.setText(phone); } @Override public void showNextPage() { String phone = etPhone.getText().toString().trim();// 注意过滤空格 if (TextUtils.isEmpty(phone)) { // Toast.makeText(this, "安全号码不能为空!", Toast.LENGTH_SHORT).show(); ToastUtils.showToast(this, "安全号码不能为空!"); return; } mPref.edit().putString("safe_phone", phone).commit();// 保存安全号码 startActivity(new Intent(this, Setup4Activity.class)); finish(); // 两个界面切换的动画 overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画 } @Override public void showPreviousPage() { startActivity(new Intent(this, Setup2Activity.class)); finish(); // 两个界面切换的动画 overridePendingTransition(R.anim.trans_previous_in, R.anim.trans_previous_out);// 进入动画和退出动画 } /** * 选择联系人 * * @param view */ public void selectContact(View view) { Intent intent = new Intent(this, ContactActivity.class);//通讯录 startActivityForResult(intent, 1);//需要有返回值 } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // System.out.println("resultCode:" + resultCode); // System.out.println("requestCode:" + requestCode); if (resultCode == Activity.RESULT_OK) { String phone = data.getStringExtra("phone"); phone = phone.replaceAll("-", "").replaceAll(" ", "");// 替换-和空格 etPhone.setText(phone);// 把电话号码设置给输入框 } super.onActivityResult(requestCode, resultCode, data); }}
ToastUtils.java
public class ToastUtils { public static void showToast(Context ctx,String text){ Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show(); }}
ContactActivity.java 通讯录
public class ContactActivity extends Activity { private ListView lvList; private ArrayList> readContact; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); lvList = (ListView) findViewById(R.id.lv_list); readContact = readContact(); // System.out.println(readContact); lvList.setAdapter(new SimpleAdapter(this, readContact, R.layout.contact_list_item, new String[] { "name", "phone" }, new int[] { R.id.tv_name, R.id.tv_phone })); //给listview设置监听 lvList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { String phone = readContact.get(position).get("phone");// 读取当前item的电话号码 Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(Activity.RESULT_OK, intent);// 将数据放在intent中返回给上一个页面 finish(); } }); } private ArrayList > readContact() { // 首先,从raw_contacts中读取联系人的id("contact_id") // 其次, 根据contact_id从data表中查询出相应的电话号码和联系人名称 // 然后,根据mimetype来区分哪个是联系人,哪个是电话号码 Uri rawContactsUri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri dataUri = Uri.parse("content://com.android.contacts/data"); ArrayList > list = new ArrayList >(); // 从raw_contacts中读取联系人的id("contact_id") //ContentProvider Cursor rawContactsCursor = getContentResolver().query(rawContactsUri, new String[] { "contact_id" }, null, null, null); if (rawContactsCursor != null) { while (rawContactsCursor.moveToNext()) { String contactId = rawContactsCursor.getString(0); // System.out.println(contactId); // 根据contact_id从data表中查询出相应的电话号码和联系人名称, 实际上查询的是视图view_data Cursor dataCursor = getContentResolver().query(dataUri, new String[] { "data1", "mimetype" }, "contact_id=?", new String[] { contactId }, null); if (dataCursor != null) { HashMap map = new HashMap (); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); // System.out.println(contactId + ";" + data1 + ";" // + mimetype); if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) { map.put("phone", data1); } else if ("vnd.android.cursor.item/name".equals(mimetype)) { map.put("name", data1); } } list.add(map); dataCursor.close(); } } rawContactsCursor.close(); } return list; }}
activity_setup3.xml
Setup4Activity.java
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); // 根据sp保存的状态,更新checkbox if (protect) { cbProtect.setText("防盗保护已经开启"); cbProtect.setChecked(true); } else { cbProtect.setText("防盗保护没有开启"); cbProtect.setChecked(false); } // 当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(); } } }); } @Override public void showNextPage() { startActivity(new Intent(this, LostFindActivity.class)); finish(); // 两个界面切换的动画 overridePendingTransition(R.anim.trans_in, R.anim.trans_out);// 进入动画和退出动画 mPref.edit().putBoolean("configed", true).commit();// 更新sp,表示已经展示过设置向导了,下次进来就不展示啦 } @Override public void showPreviousPage() { startActivity(new Intent(this, Setup3Activity.class)); finish(); // 两个界面切换的动画 overridePendingTransition(R.anim.trans_previous_in, R.anim.trans_previous_out);// 进入动画和退出动画 }}
activity_setup4.xml
SharePreferences存储在config.xml里面的内容: