Android广播接实现监听电话状态(电话的状态,拦截)
程序员文章站
2024-02-23 22:13:22
首先我们来理解下监听器的机制。
android的事件处理机制有两种:监听和回调。
a基于监听的事件处理
主要涉及三类对象:eventsource(事件源),event...
首先我们来理解下监听器的机制。
android的事件处理机制有两种:监听和回调。
a基于监听的事件处理
主要涉及三类对象:eventsource(事件源),event(事件),eventlistener(事件监听器)
监听机制处理事件的流程图如下(委派式:delegation):
1:需要在androidmanifest.xml清单中添加权限
<uses-permission android:name="android.permission.process_outgoing_calls"/> <uses-permission android:name="android.permission.read_phone_state" />
2:注册广播 要注意事件的级别 (android中的级别-1000~1000)
<receiver android:name=".phonebroadcastreceiver"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.new_outgoing_call"/> <action android:name="android.intent.action.phone_state"/> </intent-filter> </receiver>
详细配置请看 androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zyw.broadcastsendsms" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".sendsms" android:label="@string/title_activity_send_sms" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".phonebroadcastreceiver"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.new_outgoing_call"/> <action android:name="android.intent.action.phone_state"/> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.process_outgoing_calls"/> <uses-permission android:name="android.permission.read_phone_state" /> </manifest>
3:编写广播的实现类 这里需要继承broadcastreceiver 实现onreceive()方法
程序的源代码如下:
package com.zyw.broadcastsendsms; import android.app.service; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.telephony.telephonymanager; import android.util.log; public class phonebroadcastreceiver extends broadcastreceiver { private static final string tag = "message"; private static boolean mincomingflag = false; private static string mincomingnumber = null; @override public void onreceive(context context, intent intent) { // 如果是拨打电话 if (intent.getaction().equals(intent.action_new_outgoing_call)) { mincomingflag = false; string phonenumber = intent.getstringextra(intent.extra_phone_number); log.i(tag, "call out:" + phonenumber); } else { // 如果是来电 telephonymanager tmanager = (telephonymanager) context .getsystemservice(service.telephony_service); switch (tmanager.getcallstate()) { case telephonymanager.call_state_ringing: mincomingnumber = intent.getstringextra("incoming_number"); log.i(tag, "ringing :" + mincomingnumber); break; case telephonymanager.call_state_offhook: if (mincomingflag) { log.i(tag, "incoming accept :" + mincomingnumber); } break; case telephonymanager.call_state_idle: if (mincomingflag) { log.i(tag, "incoming idle"); } break; } } } /*@override public void onreceive(context context, intent intent) { string number = getresultdata(); if("5556".equals(number)){ setresultdata(null);//挂断 }else{ number = "12593"+ number; //其他,则加区号 setresultdata(number); } }*/ }
以上内容给大家介绍了android广播接实现监听电话状态(电话的状态,拦截)的相关知识,希望对大家有所帮助!
上一篇: Java对类私有变量的暴力反射技术讲解