Android 四大组件之 BroadcastReceiver_3 实现开机启动拦截电话服务

拦截电话服务(ListenCallService)参考 :https://blog.csdn.net/whjk20/article/details/112988381

这里实现开机启动完成后,启动拦截电话的服务。

目录

1.创建接收器, 处理action: Intent.ACTION_BOOT_COMPLETED

2. AndroidManifest中静态注册接收器,当应用安装时注册,收到广播时创建对象。     

3.声明权限(使用Intent.ACTION_BOOT_COMPLETED需要的权限) :

4. 输出结果

5. 注意事项:


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
再去实现能自动接收到开机完成的广播        

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值