結(jié)構(gòu)
1:監(jiān)聽來電事件
2:查詢,打開,數(shù)據(jù)庫。匹配正則表達式
3:監(jiān)聽用戶手勢
1:監(jiān)聽來電事件
1:拿到電話管理器
TelePMG = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
2:創(chuàng)建電話監(jiān)聽器類繼承PhoneStateListener
class MyPhoneStateListener extends PhoneStateListener{
//重寫電話狀態(tài)改變的方法
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state){
case TelephonyManager.CALL_STATE_IDLE:
//電話空閑狀態(tài)
System.out.println("空閑狀態(tài)");
if(mWindowManager!=null&&toast_view!=null){
//掛斷電話的時候移除吐司
mWindowManager.removeView(toast_view);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//忙碌狀態(tài),就是占線
break;
case TelephonyManager.CALL_STATE_RINGING:
//來電狀態(tài)
System.out.println("來電了號碼是"+incomingNumber);
//來電后打印吐司
showToast(incomingNumber);
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
3:創(chuàng)建電話監(jiān)聽器的對象
myPhoneStateListener = new MyPhoneStateListener();
4:使用電話管理器的listen方法綁定監(jiān)聽器
TelePMG.listen(myPhoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
5:使用完畢后記得用CLOSE方法關(guān)閉
2:查詢,打開,數(shù)據(jù)庫。匹配正則表達式
public class SelectAdressdb {
//數(shù)據(jù)庫地址
public static String PATH = "data/data/com.joker.mobilesafe/files/address.db";
private static String adress;
//封裝查詢的方法
public static String SelectAdress (String phone){
//截取電話前7位
//匹配正則表達式,不然輸入一個沒有7位的數(shù)字時候會報錯
System.out.println(phone+"```````````````");
String regular = "^1[3-8]\\d{9}";
if (phone.matches(regular)){
phone = phone.substring(0,7);
SQLiteDatabase db = SQLiteDatabase.openDatabase(PATH, null, SQLiteDatabase.OPEN_READONLY);
//表的名字,需要得到查詢結(jié)果的字段,查詢條件,查詢條件的值,是否分組,分組后查詢條件,是否排序
Cursor cursor = db.query("data1", new String[]{"outkey"}, "id=?", new String[]{phone}, null, null, null);
if (cursor.moveToNext()){
//這里傳入的0 是上面第二個條件數(shù)組的下標(biāo)值
String outkey = cursor.getString(0);
Cursor indexCursor = db.query("data2", new String[]{"location"}, "id=?", new String[]{outkey}, null, null, null);
if (indexCursor.moveToNext()){
adress = indexCursor.getString(0);
}else{
adress ="未知號碼";
}
}else {
adress ="未知號碼";
}
}else {
int length = phone.length();
switch (length){
case 3:
adress = "你難道連報警電話都不知道?";
break;
default:
adress = "不知道是哪種情況";
break;
}
}
return adress;
}
}
3:監(jiān)聽用戶手勢
1:創(chuàng)建監(jiān)聽用戶手勢的類(第二個參數(shù)傳入監(jiān)聽用戶手勢的監(jiān)聽器)
GestureDetector gestureDetector = new GestureDetector(this,SimpleOnGestureListener)
2:實現(xiàn)監(jiān)聽器的onFling方法(手勢起點參數(shù),手勢移動當(dāng)前的參數(shù),每秒x軸方向移動的像素,每秒y軸方向移動的像素)
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX()-e2.getX()>0){
//下一頁
String phone = et_phone_number.getText().toString();
if(!TextUtils.isEmpty(phone)){
Intent intent = new Intent(Set_up3.this, Set_up4.class);
startActivity(intent);
SpUtil.PutString(getApplicationContext(), FianlMath.CONTACT_MAN, phone);
finish();
overridePendingTransition(R.anim.next_anim_in,R.anim.next_anim_out);
}else {
Toast.makeText(Set_up3.this,"請輸入聯(lián)系人",Toast.LENGTH_SHORT).show();
}
}else if (e1.getX()-e2.getX()<0){
//上一頁
Intent intent = new Intent(Set_up3.this, Set_up2.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.up_anim_in,R.anim.up_anim_out);
}
return super.onFling(e1, e2, velocityX, velocityY);
}
重寫ontouchevent方法(這個方法用來捕捉用戶的手勢)
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}