首先我们来理解下监听器的机制。
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-sdkandroID:minSdkVersion="8"androID:targetSdkVersion="15" /><applicationandroID:icon="@drawable/ic_launcher"androID:label="@string/app_name"androID:theme="@style/Apptheme" ><activityandroID: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;@OverrIDepublic 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;}}}/*@OverrIDepublic voID onReceive(Context context,Intent intent) {String number = getResultData();if("5556".equals(number)){setResultData(null);//挂断}else{number = "12593"+ number; //其他,则加区号setResultData(number);}}*/}
以上内容给大家介绍了AndroID广播接实现监听电话状态(电话的状态,拦截)的相关知识,希望对大家有所帮助!
总结以上是内存溢出为你收集整理的Android广播接实现监听电话状态(电话的状态,拦截)全部内容,希望文章能够帮你解决Android广播接实现监听电话状态(电话的状态,拦截)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)