首页 > 系统 > Android > 正文

Android 代码设置开机自启动App的方法

2019-10-21 21:44:22
字体:
来源:转载
供稿:网友

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

代码如下:

创建一个监听。

/** * 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 。

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

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表