android 呼出电话的监听(去电监听)

android 呼出电话的监听(去电监听)

 

 

权限:

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

 

通过接收呼出电话的广播来实现

 

定义广播类

 

package com.zhouzijing.android.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

public class BroadcastReceiverMgr extends BroadcastReceiver {
	
	private final String TAG = MyBroadcastReceiver.TAG;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.i(TAG, "[Broadcast]"+action);
		

		
		//呼出电话
		if(action.equals(MyBroadcastReceiver.B_ACTION_NEW_OUTGOING_CALL)){
			String outPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
			Log.i(TAG, "[Broadcast]ACTION_NEW_OUTGOING_CALL:"+outPhoneNumber);
			//this.setResultData(null);
//这里可以更改呼出电话号码。如果设置为null,电话就永远不会播出了.
		}
		
		
	}
	
	
}

 定义activity类

package com.zhouzijing.android.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;

public class MyBroadcastReceiver extends Activity {
	public final static String TAG = "MyBroadcastReceiver";
	
	
	public final static String B_ACTION_NEW_OUTGOING_CALL = Intent.ACTION_NEW_OUTGOING_CALL;
	
	private BroadcastReceiverMgr mBroadcastReceiver;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_broadcast_receiver);
	}
	
	
	
	

	//按钮1-注册广播
	public void registerIt(View v) {
		Log.i(TAG, "registerIt");
		mBroadcastReceiver = new BroadcastReceiverMgr();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
		intentFilter.setPriority(Integer.MAX_VALUE);
		registerReceiver(mBroadcastReceiver, intentFilter);
	}
	
	//按钮2-撤销广播
	public void unregisterIt(View v) {
		Log.i(TAG, "unregisterIt");
		unregisterReceiver(mBroadcastReceiver);
	}
	
}
 

 

你可能感兴趣的:(android)