我是靠谱客的博主 善良超短裙,这篇文章主要介绍Android 代码设置开机自启动App的方法,现在分享给大家,希望可以做个参考。

有的时候想要用户一旦打开手机。我们的APP就自动运行了。

代码如下:

创建一个监听。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/** * create by:sunlei on 2017/7/7 15:48 * e-mail:872822645@qq.com * introduce: */ public class ContentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent it=new Intent(context,MainActivity.class); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(it); Toast.makeText(context,"我自启动成功了哈",Toast.LENGTH_LONG).show(); } }

注意:如果是页面跳转。此处必须加上flags 。

在配置文件增加权限和注册此广播:

复制代码
1
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
复制代码
1
2
3
4
5
6
7
8
<receiver android:name=".ContentReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.HOME" /> </intent-filter> </receiver>

此处注册了此广播。用来监听。。2个 category 分别是 home 和 launcher 2个都可以。。2选1即可

最后注意。大部分手机都有管家类软件限制了不允许开机自启动。。所以如果没有效果。需要设置允许。

我用的是小米NOTE手机测试。乐视2手机。亲测有效!

以上这篇Android 代码设置开机自启动App的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是善良超短裙最近收集整理的关于Android 代码设置开机自启动App的方法的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(157)

评论列表共有 0 条评论

立即
投稿
返回
顶部