在系统中添加监听开机完成广播的功能,在接收到开机广播后启动第三方app
frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
<receiver
android:name=".receiver.StartSelfReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
在app中使用的范例
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.pne.kotlin.ReplaceIconActivity;
public class StartSelfReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Android设备开机时会发送一条开机广播:"android.intent.action.BOOT_COMPLETED"
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Intent selfIntent = new Intent(context, ReplaceIconActivity.class);
selfIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(selfIntent);
}
}
}
4/PhoneWindowManager.java中启动第三方app的功能实现
/** {@inheritDoc} */
@Override
public void systemReady() {
// In normal flow, systemReady is called before other system services are ready.
// So it is better not to bind keyguard here.
mKeyguardDelegate.onSystemReady();
mVrManagerInternal = LocalServices.getService(VrManagerInternal.class);
if (mVrManagerInternal != null) {
mVrManagerInternal.addPersistentVrModeStateListener(mPersistentVrModeListener);
}
readCameraLensCoverState();
updateUiMode();
mDefaultDisplayRotation.updateOrientationListener();
synchronized (mLock) {
mSystemReady = true;
mHandler.post(new Runnable() {
@Override
public void run() {
updateSettings();
}
});
// If this happens, for whatever reason, systemReady came later than systemBooted.
// And keyguard should be already bound from systemBooted
if (mSystemBooted) {
mKeyguardDelegate.onBootCompleted();
}
}
//add code start
IntentFilter intentFilter = new IntentFilter("android.intent.action.BOOT_COMPLETED");
mContext.registerReceiver(mWallpaperChangedReceiver, intentFilter);
//add code end
mAutofillManagerInternal = LocalServices.getService(AutofillManagerInternal.class);
}
/** {@inheritDoc} */
@Override
public void systemBooted() {
bindKeyguard();
synchronized (mLock) {
mSystemBooted = true;
if (mSystemReady) {
mKeyguardDelegate.onBootCompleted();
}
}
startedWakingUp(ON_BECAUSE_OF_UNKNOWN);
finishedWakingUp(ON_BECAUSE_OF_UNKNOWN);
screenTurningOn(null);
screenTurnedOn();
}
// add start
BroadcastReceiver mWallpaperChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("android.intent.action.BOOT_COMPLETED")) {
Slog.d("WindowManager", "android.intent.action.BOOT_COMPLETED");
Intent selfIntent = new Intent(context, ReplaceIconActivity.class);
selfIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(selfIntent);
}
}
};
//add end