Android编程中黑名单的实现方法
本文实例讲述了Android编程中黑名单的实现方法。分享给大家供大家参考,具体如下:
说明:由于挂断电话android api不是对外开放的,所以需要使用反射的方法得到拨打电话的服务。
1.将android源代码中的"aidl"文件拷贝到项目中
这样项目中会生成两个包:android.telephony;此包中文件为:NeighboringCellInfo.aidl
com.android.internal.telephony;此包中文件为:ITelephony.aidl
2.通过反射挂断电话;代码如下:
/**
*挂断电话
*/
publicvoidendCall(){
try{
Methodmethod=Class.forName("android.os.ServiceManager").getMethod("getService",String.class);
IBinderbinder=(IBinder)method.invoke(null,newObject[]{TELEPHONY_SERVICE});
ITelephonytelephony=ITelephony.Stub.asInterface(binder);
telephony.endCall();
}catch(Exceptione){
e.printStackTrace();
}
}
3.删除通话记录中的记录
/**
*删除呼叫记录
*/
publicvoiddeleteCallLog(StringincomingNumber){
ContentResolverresolver=getContentResolver();
Cursorcursor=resolver.query(CallLog.Calls.CONTENT_URI,null,"number=?",newString[]{incomingNumber},null);
if(cursor.moveToNext()){
Stringid=cursor.getString(cursor.getColumnIndex("_id"));
resolver.delete(CallLog.Calls.CONTENT_URI,"_id=?",newString[]{id});
}
}
4.直接这样调用是不能删除电话记录的,因为生成电话记录的过程是一个异步的过程,在挂断电话之后不能立即删除电话记录,所以这里要使用ContentObserver(内容观察者)
privateclassMyObserverextendsContentObserver{
privateStringincomingNumber;
publicMyObserver(Handlerhandler,StringincomingNumber){
super(handler);
this.incomingNumber=incomingNumber;
}
@Override
publicvoidonChange(booleanselfChange){
super.onChange(selfChange);
deleteCallLog(incomingNumber);
getContentResolver().unregisterContentObserver(this);
}
}
6.最后把整个service代码贴到下面
publicclassAddressServiceextendsService{
privatestaticfinalStringTAG="AddressService";
privateTelephonyManagermanager;
privateMyPhoneListenerlistener;
privateWindowManagerwManager;
privateViewview;
privateSharedPreferencessp;
longstartTime=0;
longendTime=0;
privateBlackNumberDaodao;
@Override
publicIBinderonBind(Intentarg0){
returnnull;
}
/**
*服务第一次被创建的时候调用的方法
*服务被初始化时调用的方法
*/
@Override
publicvoidonCreate(){
super.onCreate();
listener=newMyPhoneListener();
manager=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
wManager=(WindowManager)this.getSystemService(WINDOW_SERVICE);
manager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
sp=getSharedPreferences("config",MODE_PRIVATE);
dao=newBlackNumberDao(this);
//if(3000>(endTime-startTime)){
//Stringns=Context.NOTIFICATION_SERVICE;
//NotificationManagermNotificationManager=(NotificationManager)getSystemService(ns);
////定义通知栏展现的内容信息
//inticon=R.drawable.icon5;
//CharSequencetickerText="我的通知栏标题";
//longwhen=System.currentTimeMillis();
//Notificationnotification=newNotification(icon,tickerText,when);
////定义下拉通知栏时要展现的内容信息
//Contextcontext=getApplicationContext();
//CharSequencecontentTitle="我的通知栏标展开标题";
//CharSequencecontentText="我的通知栏展开详细内容";
//IntentnotificationIntent=newIntent(AddressService.this,BootStartDemo.class);
//PendingIntentcontentIntent=PendingIntent.getActivity(AddressService.this,0,notificationIntent,0);
//notification.setLatestEventInfo(context,contentTitle,contentText,contentIntent);
////用mNotificationManager的notify方法通知用户生成标题栏消息通知
//mNotificationManager.notify(1,notification);
//}
}
/**
*服务停止的时候调用
*/
@Override
publicvoidonDestroy(){
super.onDestroy();
manager.listen(listener,PhoneStateListener.LISTEN_NONE);
listener=null;
}
privateclassMyPhoneListenerextendsPhoneStateListener{
/**
*电话状态发生改变的时候调用的方法
*/
@Override
publicvoidonCallStateChanged(intstate,StringincomingNumber){
super.onCallStateChanged(state,incomingNumber);
switch(state){
caseTelephonyManager.CALL_STATE_IDLE:
if(null!=view){
wManager.removeView(view);
view=null;
}
endTime=System.currentTimeMillis();
break;
caseTelephonyManager.CALL_STATE_RINGING://零响状态
//判断number是否在黑名单中
if(dao.find(incomingNumber)){
//挂断电话
endCall();
//删除呼叫记录
//deleteCallLog(incomingNumber);
ContentResolverresolver=getContentResolver();
resolver.registerContentObserver(CallLog.Calls.CONTENT_URI,true,newMyObserver(newHandler(),incomingNumber));
}
Log.i(TAG,"来电号码为"+incomingNumber);
Stringaddress=NumberAddressService.getAddress(incomingNumber);
Log.i(TAG,"归属地为"+address);
showLocation(address);
//获取当前系统的时间
startTime=System.currentTimeMillis();
break;
caseTelephonyManager.CALL_STATE_OFFHOOK://接通电话状态
break;
}
}
}
/**
*在窗体上显示出来位置信息
*@paramaddress
*/
publicvoidshowLocation(Stringaddress){
WindowManager.LayoutParamsparams=newWindowManager.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;
intx=sp.getInt("lastx",0);
inty=sp.getInt("lasty",0);
params.x=x;
params.y=y;
view=View.inflate(getApplicationContext(),R.layout.show_location,null);
LinearLayoutll_location=(LinearLayout)view.findViewById(R.id.ll_location);
TextViewtv_location=(TextView)view.findViewById(R.id.tv_location);
intbackground=sp.getInt("background",0);
if(0==background){
ll_location.setBackgroundResource(R.drawable.call_locate_gray);
}elseif(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);
}
/**
*删除呼叫记录
*/
publicvoiddeleteCallLog(StringincomingNumber){
ContentResolverresolver=getContentResolver();
Cursorcursor=resolver.query(CallLog.Calls.CONTENT_URI,null,"number=?",newString[]{incomingNumber},null);
if(cursor.moveToNext()){
Stringid=cursor.getString(cursor.getColumnIndex("_id"));
resolver.delete(CallLog.Calls.CONTENT_URI,"_id=?",newString[]{id});
}
}
/**
*挂断电话
*/
publicvoidendCall(){
try{
Methodmethod=Class.forName("android.os.ServiceManager").getMethod("getService",String.class);
IBinderbinder=(IBinder)method.invoke(null,newObject[]{TELEPHONY_SERVICE});
ITelephonytelephony=ITelephony.Stub.asInterface(binder);
telephony.endCall();
}catch(Exceptione){
e.printStackTrace();
}
}
privateclassMyObserverextendsContentObserver{
privateStringincomingNumber;
publicMyObserver(Handlerhandler,StringincomingNumber){
super(handler);
this.incomingNumber=incomingNumber;
}
@Override
publicvoidonChange(booleanselfChange){
super.onChange(selfChange);
deleteCallLog(incomingNumber);
getContentResolver().unregisterContentObserver(this);
}
}
}
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android通信方式总结》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。