场景一:样机开机后自启动
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnandroid.myapplication">
<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=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
MyReceiver.java
package com.learnandroid.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent intent1 = new Intent(context, MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
参考资料:
android 开机自启动 apk_望那伊人的博客-CSDN博客_安卓自启动
关于安卓开机自启动的方案(最高测试9.0系统管用)_wz_1992的博客-CSDN博客_安卓开机自动启动
场景二:样机连网后自启动
场景三:样机连接WLAN网络后自启动
参考资料:
【Android】Broadcast标准(无序)广播监听网络WiFi或移动数据改变状态(动态注册)、实现APP开机自启(静态注册)_冰冷的希望的博客-CSDN博客
后台执行限制 ?|? Android 开发者 ?|? Android Developers
隐式广播例外情况 ?|? Android 开发者 ?|? Android Developers
|