方案:
第一步:接收“开机完成”广播:android.intent.action.BOOT_COMPLETED;
第二步:在广播接收器中启动HelloApp:
AndroidManifest.xml:
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:enabled="true"
android:exported="true">
广播接收器:
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注册广播过滤器:
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:enabled="true"
android:exported="true">
5.将编译出来的apk放到/system/app/下面,然后重启
6.效果
Github代码:https://github.com/menghaocheng/HelloApp
(完)