Android boot directly runs app and serves as an example of mobile phone desktop

  • 2021-10-13 08:29:47
  • OfStack

Directly on the code:

1. Boot up APP

1.1 Write a broadcast receiver to receive the mobile phone boot broadcast


`public class Receiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {

 Log.e("broadCastReceiver","onReceiver...");

 try {
  Intent mBootIntent = new Intent(context, MainActivity.class);
  //  The following sentence must be added to boot and run automatically app Interface of 
  mBootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(mBootIntent);
 } catch (Exception e) {
  e.printStackTrace();
 }
 }

}`

1.2 Statically Registered Broadcast Receiver in manifest


 <!-- Boot broadcast receiver -->
 <receiver android:name=".Receiver">
 <intent-filter>
  <!-- Register the boot broadcast address -->
  <action android:name="android.intent.action.BOOT_COMPLETED"/>
 </intent-filter>
 </receiver>

2. Start up and run app directly, and regard the main page of app as the desktop of mobile phone

Remarks: As mentioned in 1 above, the system desktop will be displayed first after the mobile phone is turned on, and app can only be started after 1 meeting. What should be mentioned below is that the app Launcher page will be directly used as the mobile phone desktop when the mobile phone is turned on, and app will be started directly when the mobile phone is turned on, without waiting.


 <activity android:name=".MainActivity">
 <intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER"/>
  <category android:name="android.intent.category.HOME" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.MONKEY"/>
 </intent-filter>
 </activity>

manifest sets the main activity as described above

At this time, the broadcast receiver registered in 1 can actually be discarded, which may have something to do with the mobile phone. You can try to stay or not.

(Many people think it's ok when they come here. As a result, restarting the mobile phone still doesn't work. Look below)

Here, there is only one key step away from success, that is, you need to find desktop settings options in mobile phone settings. If you can't find them, you can search 1 in the search bar in the settings. After finding them, you will find that there is one more option in desktop settings, that is, your app, because it is added < category android:name="android.intent.category.HOME" / > In this line, the desktop setting will also regard your app as a desktop theme, and there is also an option for the system desktop. You need to set it to your own app, restart your mobile phone, ok!

(I tested Huawei 5.1, Huawei 7.0 and Xiaomi 5.23 phones, and I can find the desktop setting option.)

If you can't find desktop settings options, you can set up applications from your mobile phone-view all applications (including system applications), find applications with words such as desktop programs, and know their default settings. Clear and go back to APP. When you press back to the main page, you should have a setting item similar to selecting the default desktop.

If you solve your problem, please praise and support it!


Related articles: