结构
1:监听来电事件
2:查询,打开,数据库。匹配正则表达式
3:监听用户手势
1:监听来电事件
1:拿到电话管理器
TelePMG = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
2:创建电话监听器类继承PhoneStateListener
class MyPhoneStateListener extends PhoneStateListener{
//重写电话状态改变的方法
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state){
case TelephonyManager.CALL_STATE_IDLE:
//电话空闲状态
System.out.println("空闲状态");
if(mWindowManager!=null&&toast_view!=null){
//挂断电话的时候移除吐司
mWindowManager.removeView(toast_view);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//忙碌状态,就是占线
break;
case TelephonyManager.CALL_STATE_RINGING:
//来电状态
System.out.println("来电了号码是"+incomingNumber);
//来电后打印吐司
showToast(incomingNumber);
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
3:创建电话监听器的对象
myPhoneStateListener = new MyPhoneStateListener();
4:使用电话管理器的listen方法绑定监听器
TelePMG.listen(myPhoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
5:使用完毕后记得用CLOSE方法关闭
2:查询,打开,数据库。匹配正则表达式
public class SelectAdressdb {
//数据库地址
public static String PATH = "data/data/com.joker.mobilesafe/files/address.db";
private static String adress;
//封装查询的方法
public static String SelectAdress (String phone){
//截取电话前7位
//匹配正则表达式,不然输入一个没有7位的数字时候会报错
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);
//表的名字,需要得到查询结果的字段,查询条件,查询条件的值,是否分组,分组后查询条件,是否排序
Cursor cursor = db.query("data1", new String[]{"outkey"}, "id=?", new String[]{phone}, null, null, null);
if (cursor.moveToNext()){
//这里传入的0 是上面第二个条件数组的下标值
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:监听用户手势
1:创建监听用户手势的类(第二个参数传入监听用户手势的监听器)
GestureDetector gestureDetector = new GestureDetector(this,SimpleOnGestureListener)
2:实现监听器的onFling方法(手势起点参数,手势移动当前的参数,每秒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,"请输入联系人",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);
}