代码如下:
创建一个监听。
/** * create by:sunlei on 2017/7/7 15:48 * e-mail:[email protected] * 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的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持华域联盟。
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)