DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 移动应用 > Android

Android广播接实现电话的监听

Android广播接实现电话的监听  

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   


<manifestxmlns: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><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity><receiverandroid:name=".PhoneBroadcastReceiver"><intent-filterandroid:priority="1000"><actionandroid:name="android.intent.action.NEW_OUTGOING_CALL"/><actionandroid:name="android.intent.action.PHONE_STATE"/></intent-filter></receiver></application><uses-permissionandroid:name="android.permission.PROCESS_OUTGOING_CALLS"/><uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/></manifest>

 

 

3:编写广播的实现类 这里需要继承BroadcastReceiver 实现onReceive()方法

 

程序的源代码如下:


packagecom.zyw.broadcastsendsms;importandroid.app.Service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.telephony.TelephonyManager;importandroid.util.Log;publicclassPhoneBroadcastReceiverextendsBroadcastReceiver {privatestaticfinalString TAG = "message";privatestaticbooleanmIncomingFlag =false;privatestaticString mIncomingNumber =null; @OverridepublicvoidonReceive(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()) {caseTelephonyManager.CALL_STATE_RINGING: mIncomingNumber= intent.getStringExtra("incoming_number"); Log.i(TAG,"RINGING :" +mIncomingNumber);break;caseTelephonyManager.CALL_STATE_OFFHOOK:if(mIncomingFlag) { Log.i(TAG,"incoming ACCEPT :" +mIncomingNumber); }break;caseTelephonyManager.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); } }*/}

 

 

 

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号