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);
	}
	
}
 

 

发布了824 篇原创文章 · 获赞 13 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览