Android编程中黑名单的实现方法
程序员文章站
2024-02-21 13:20:04
本文实例讲述了android编程中黑名单的实现方法。分享给大家供大家参考,具体如下:
说明:由于挂断电话android api不是对外开放的,所以...
本文实例讲述了android编程中黑名单的实现方法。分享给大家供大家参考,具体如下:
说明:由于挂断电话android api不是对外开放的,所以需要使用反射的方法得到拨打电话的服务。
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.直接这样调用是不能删除电话记录的,因为生成电话记录的过程是一个异步的过程,在挂断电话之后不能立即删除电话记录,所以这里要使用contentobserver(内容观察者)
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; } /** * 服务第一次被创建的时候调用的方法 * 服务被初始化时调用的方法 */ @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); // //定义通知栏展现的内容信息 // int icon = r.drawable.icon5; // charsequence tickertext = "我的通知栏标题"; // long when = system.currenttimemillis(); // notification notification = new notification(icon, tickertext, when); // //定义下拉通知栏时要展现的内容信息 // context context = getapplicationcontext(); // charsequence contenttitle = "我的通知栏标展开标题"; // charsequence contenttext = "我的通知栏展开详细内容"; // 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方法通知用户生成标题栏消息通知 // mnotificationmanager.notify(1, notification); // } } /** * 服务停止的时候调用 */ @override public void ondestroy() { super.ondestroy(); manager.listen(listener, phonestatelistener.listen_none); listener = null; } private class myphonelistener extends phonestatelistener{ /** * 电话状态发生改变的时候调用的方法 */ @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: // 零响状态 //判断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); //获取当前系统的时间 starttime = system.currenttimemillis(); break; case telephonymanager.call_state_offhook: //接通电话状态 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); } } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android通信方式总结》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: Android开发中应用程序分享功能实例
下一篇: Android实现图片缓存与异步加载
推荐阅读
-
Android编程中黑名单的实现方法
-
一个Activity中多个Fragment实现沉浸式状态栏的解决方法
-
Android仿微信进度弹出框的实现方法
-
Android LayoutInflater的inflate方法中attachToRoot的作用
-
用SQL实现统计报表中的"小计"与"合计"的方法详解
-
Android LayoutInflater的inflate方法中attachToRoot的作用
-
Android重写TextView实现文字整齐排版的方法(附demo源码下载)
-
Android实现取消GridView中Item选中时默认的背景色
-
Android应用开发中触摸屏手势识别的实现方法解析
-
Android通过反射实现强制停止应用程序的方法