当前位置:   article > 正文

Android实现开机自启_android 开机启动

android 开机启动

有一些服务比如推送服务,想实现开机自启,怎么实现呢。其实很简单,系统开机后会发送一个广播,我们只需要在自己的APP中注册一个BroadcastReceiver来接收就可以了。

1、自定义一个BroadcastReceiver,这里命名为BootReceiver

package com.example.androidtest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BootReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO start your own service
        Toast.makeText(context, "接收到开机启动广播", Toast.LENGTH_LONG).show();
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2、在Manifest文件中application节点中注册广播接收器

<receiver android:name="com.example.androidtest.BootReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
  </intent-filter>
 </receiver>
  • 1
  • 2
  • 3
  • 4
  • 5

3、声明权限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  • 1

4、测试一下

这里写图片描述

安装应用后重启模拟器,看到有Toast

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/712816
推荐阅读
相关标签
  

闽ICP备14008679号