拦截电话服务(ListenCallService)参考 :https://blog.csdn.net/whjk20/article/details/112988381
这里实现开机启动完成后,启动拦截电话的服务。
目录
1.创建接收器, 处理action: Intent.ACTION_BOOT_COMPLETED
2. AndroidManifest中静态注册接收器,当应用安装时注册,收到广播时创建对象。
3.声明权限(使用Intent.ACTION_BOOT_COMPLETED需要的权限) :
1.创建接收器, 处理action: Intent.ACTION_BOOT_COMPLETED
即启动ListenCallService (使用onStartService , 什么时候停止??)
//静态注册,当应用安装时注册,收到广播时创建对象。
public class BootCompleteReceiver1 extends BroadcastReceiver {
private static final String TAG = "BootCompleteReceiver1";
private void log(String msg) {
Log.d(TAG, msg);
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
log("onReceive intentAction=" + intentAction);
Intent callIntent = new Intent(context, ListenCallService.class);
context.startService(callIntent);
log("start ListenCallService done");
}
}
2. AndroidManifest中静态注册接收器,当应用安装时注册,收到广播时创建对象。
<receiver android:name=".BootCompleteReceiver1">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
3.声明权限(使用Intent.ACTION_BOOT_COMPLETED需要的权限) :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
4. 输出结果
01-27 11:04:36.961 D BootCompleteReceiver1: onReceive intentAction=android.intent.action.BOOT_COMPLETED
01-27 11:04:36.970 D BootCompleteReceiver1: start ListenCallService done
01-27 11:04:36.986 D ListenCallService: onCreate
01-27 11:04:37.006 D ListenCallService: onCreate - already granted permission.READ_CALL_LOG
01-27 11:04:37.008 D ListenCallService: onStartCommand
01-27 11:04:37.008 D ListenCallService: onCallStateChanged state=0, phoneNumber=
01-27 11:04:55.328 D ListenCallService: onCallStateChanged state=1, phoneNumber=10086
01-27 11:04:55.450 D ListenCallService: onCallStateChanged endCall ret=true
01-27 11:04:55.843 D ListenCallService: onCallStateChanged state=0, phoneNumber=10086
5. 注意事项:
(1) 需要保证开机后,应用已经跑起来了,否则有可能不会收到广播 (这里开机后手动启动了应用)
后续可以参考:https://stackoverflow.com/questions/20441308/boot-completed-not-working-android
再去实现能自动接收到开机完成的广播