Android編程中黑名單的實現(xiàn)方法
本文實例講述了Android編程中黑名單的實現(xiàn)方法。分享給大家供大家參考,具體如下:
說明:由于掛斷電話android api不是對外開放的,所以需要使用反射的方法得到撥打電話的服務(wù)。
1.將android源代碼中的"aidl"文件拷貝到項目中
這樣項目中會生成兩個包:android.telephony;此包中文件為:NeighboringCellInfo.aidl
com.android.internal.telephony;此包中文件為:ITelephony.aidl
2.通過反射掛斷電話;代碼如下:
/**
* 掛斷電話
*/
public void endCall() {
try {
Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
ITelephony telephony = ITelephony.Stub.asInterface(binder);
telephony.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
3.刪除通話記錄中的記錄
/**
* 刪除呼叫記錄
*/
public void deleteCallLog(String incomingNumber) {
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null,"number=?", new String[]{incomingNumber}, null);
if(cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("_id"));
resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{id});
}
}
4.直接這樣調(diào)用是不能刪除電話記錄的,因為生成電話記錄的過程是一個異步的過程,在掛斷電話之后不能立即刪除電話記錄,所以這里要使用ContentObserver(內(nèi)容觀察者)
private class MyObserver extends ContentObserver{
private String incomingNumber;
public MyObserver(Handler handler,String incomingNumber) {
super(handler);
this.incomingNumber = incomingNumber;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
deleteCallLog(incomingNumber);
getContentResolver().unregisterContentObserver(this);
}
}
6.最后把整個service代碼貼到下面
public class AddressService extends Service{
private static final String TAG = "AddressService";
private TelephonyManager manager;
private MyPhoneListener listener;
private WindowManager wManager;
private View view;
private SharedPreferences sp;
long startTime = 0;
long endTime = 0;
private BlackNumberDao dao;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
/**
* 服務(wù)第一次被創(chuàng)建的時候調(diào)用的方法
* 服務(wù)被初始化時調(diào)用的方法
*/
@Override
public void onCreate() {
super.onCreate();
listener = new MyPhoneListener();
manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
wManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
sp = getSharedPreferences("config", MODE_PRIVATE);
dao = new BlackNumberDao(this);
// if(3000>(endTime - startTime)){
// String ns = Context.NOTIFICATION_SERVICE;
// NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
// //定義通知欄展現(xiàn)的內(nèi)容信息
// int icon = R.drawable.icon5;
// CharSequence tickerText = "我的通知欄標(biāo)題";
// long when = System.currentTimeMillis();
// Notification notification = new Notification(icon, tickerText, when);
// //定義下拉通知欄時要展現(xiàn)的內(nèi)容信息
// Context context = getApplicationContext();
// CharSequence contentTitle = "我的通知欄標(biāo)展開標(biāo)題";
// CharSequence contentText = "我的通知欄展開詳細內(nèi)容";
// Intent notificationIntent = new Intent(AddressService.this,BootStartDemo.class);
// PendingIntent contentIntent = PendingIntent.getActivity(AddressService.this, 0,notificationIntent, 0);
// notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);
// //用mNotificationManager的notify方法通知用戶生成標(biāo)題欄消息通知
// mNotificationManager.notify(1, notification);
// }
}
/**
* 服務(wù)停止的時候調(diào)用
*/
@Override
public void onDestroy() {
super.onDestroy();
manager.listen(listener, PhoneStateListener.LISTEN_NONE);
listener = null;
}
private class MyPhoneListener extends PhoneStateListener{
/**
* 電話狀態(tài)發(fā)生改變的時候調(diào)用的方法
*/
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if(null != view){
wManager.removeView(view);
view = null;
}
endTime = System.currentTimeMillis();
break;
case TelephonyManager.CALL_STATE_RINGING: // 零響狀態(tài)
//判斷number是否在黑名單中
if(dao.find(incomingNumber)){
//掛斷電話
endCall();
//刪除呼叫記錄
// deleteCallLog(incomingNumber);
ContentResolver resolver = getContentResolver();
resolver.registerContentObserver(CallLog.Calls.CONTENT_URI, true, new MyObserver(new Handler(),incomingNumber));
}
Log.i(TAG,"來電號碼為"+ incomingNumber);
String address = NumberAddressService.getAddress(incomingNumber);
Log.i(TAG,"歸屬地為"+ address);
showLocation(address);
//獲取當(dāng)前系統(tǒng)的時間
startTime = System.currentTimeMillis();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //接通電話狀態(tài)
break;
}
}
}
/**
* 在窗體上顯示出來位置信息
* @param address
*/
public void showLocation(String address) {
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.setTitle("Toast");
params.gravity = Gravity.LEFT | Gravity.TOP;
int x = sp.getInt("lastx", 0);
int y = sp.getInt("lasty", 0);
params.x = x;
params.y = y;
view = View.inflate(getApplicationContext(), R.layout.show_location, null);
LinearLayout ll_location = (LinearLayout) view.findViewById(R.id.ll_location);
TextView tv_location = (TextView) view.findViewById(R.id.tv_location);
int background = sp.getInt("background", 0);
if(0 == background){
ll_location.setBackgroundResource(R.drawable.call_locate_gray);
}else if(1 == background){
ll_location.setBackgroundResource(R.drawable.call_locate_orange);
}else {
ll_location.setBackgroundResource(R.drawable.call_locate_green);
}
tv_location.setText(address);
tv_location.setTextSize(24);
wManager.addView(view, params);
}
/**
* 刪除呼叫記錄
*/
public void deleteCallLog(String incomingNumber) {
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null,"number=?", new String[]{incomingNumber}, null);
if(cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("_id"));
resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{id});
}
}
/**
* 掛斷電話
*/
public void endCall() {
try {
Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
ITelephony telephony = ITelephony.Stub.asInterface(binder);
telephony.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
private class MyObserver extends ContentObserver{
private String incomingNumber;
public MyObserver(Handler handler,String incomingNumber) {
super(handler);
this.incomingNumber = incomingNumber;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
deleteCallLog(incomingNumber);
getContentResolver().unregisterContentObserver(this);
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android通信方式總結(jié)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
android 仿微信demo——微信啟動界面實現(xiàn)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06
Android自定義可循環(huán)的滾動選擇器CycleWheelView
Android自定義可循環(huán)的滾動選擇器CycleWheelView替代TimePicker/NumberPicker/WheelView,很實用的一篇文章分享給大家,感興趣的小伙伴們可以參考一下2016-07-07

