1.通过注册广播接收者监听用户拨打电话操作;
2.当用户拨打电话时,广播接收者接收到号码,并与指定的”暗号“对比,若匹配,则启动某个界面并且终止用户拨打电话操作。
实现:
1.在清单文件中配置广播接收者,并添加权限:
<receiver android:name="cn.edu.chd.mobilesafe.receiver.CallPhoneReceiver" > <intent-filter android:priority="1000" > <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
2.在广播接收者OnReceive方法中进行处理:
public class CallPhoneReceiver extends BroadcastReceiver { private static final String CODE = "110"; @Override public void onReceive(Context context, Intent intent) { String number = getResultData(); if(CODE.equals(number)) { Intent myIntent = new Intent(context,DemoActivity.class); myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(myIntent); //终止电话外拨 setResultData(null); } } }