方案:
第一步:接收“开机完成”广播:android.intent.action.BOOT_COMPLETED;
第二步:在广播接收器中启动HelloApp:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.excample.helloapp"> <!--声明接收启动完成广播的权限--> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="1000"> <!--.接收启动完成的广播--> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application> </manifest>
广播接收器:
package com.excample.helloapp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){ Intent thisIntent = new Intent(context, MainActivity.class); thisIntent.setAction("android.intent.action.MAIN"); thisIntent.addCategory("android.intent.category.LAUNCHER"); thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(thisIntent); } } }
------------------------------------------------------------------------------------------------------------------------
图文详解:
首先,你得有一个APP!
创建HelloApp:
1.新建项目,取名HelloApp
2.下一步(保持默认)
3.下一步(保持默认)
4.下一步(保持默认)
5.修改图示为”Project"(可选)
6.Project图示效果:
7.运行
8.运行效果:
设置HelloApp开机启动:
1.
2.
3.在广播接收器中启动HelloApp:
package com.excample.helloapp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){ Intent thisIntent = new Intent(context, MainActivity.class); thisIntent.setAction("android.intent.action.MAIN"); thisIntent.addCategory("android.intent.category.LAUNCHER"); thisIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(thisIntent); } } }
4.修改AndroidManifest.xml注册广播过滤器:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.excample.helloapp"> <!--声明接收启动完成广播的权限--> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="true"> <intent-filter> <!--.接收启动完成的广播--> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application> </manifest>
5.将编译出来的apk放到/system/app/下面,然后重启
6.效果
Github代码:https://github.com/menghaocheng/HelloApp
(完)