Android's inability to receive boot-up broadcasts (SD card)

Keywords: Android Attribute xml Mobile

Copyright Statement: This article is the original article of the blogger. It can not be reproduced without the permission of the blogger.

Android When the mobile phone is turned on, it will send android.intent.action.BOOT_COMPLETED broadcasting, which can be monitored by listening to the broadcasting.


The general steps are as follows:

1. Registered Broadcasting

  1. <receiver android:name="com.netmoon.broadcast.BootBroadCastReceiver">  
  2.     <intent-filter>  
  3.         <action android:name="android.intent.action.BOOT_COMPLETED">  
  4.         </action>  
  5.     </intent-filter>  
  6. </receiver>  

2. Adding permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Implementing Receiver

  1. public class BootRroadCastReceiver extends BroadcastReceiver {  
  2.     private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         if(ACTION_BOOT.equals(intent.getAction()))  
  6.             Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();  
  7.     }  
  8. }  

But when Android API Level 8 is above, the program can be installed on the SD card. If the program is installed on the SD card, the SD card will not be mounted until the BOOT_COMPLETED broadcast is sent, so the program can not monitor the broadcast.


Solution: Monitor boot and sd card mounting at the same time. (You can't just listen to the mount and think it's booting, because there's no sd card on the phone.)

The implementation of media mounted monitoring for mounting is as follows:

  1. <receiver android:name=".Ge">  
  2.     <intent-filter >  
  3.         <action android:name="android.intent.action.MEDIA_MOUNTED"/>  
  4.         <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>  
  5.         <data android:scheme="file">  
  6.         </data>  
  7.     </intent-filter>  
  8. </receiver>  

Receiver, which monitors media mounted, does not write.

How to implement a broadcasting receiver that can simultaneously monitor boot BOOT_COMPLETED and mount media mounted?

In theory, just put the intent-filter of media mounted and the intent-filter of BOOT_COMPLETED together, but in the same intent-filter, boot complete can't listen and needs to be placed in two intent filter s:

  1. <receiver android:name=".Ge">          
  2.     <intent-filter >  
  3.         <action android:name="android.intent.action.BOOT_COMPLETED"/>  
  4.     </intent-filter>  
  5.     <intent-filter >  
  6.         <action android:name="android.intent.action.MEDIA_MOUNTED"/>  
  7.         <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>  
  8.         <data android:scheme="file">  
  9.         </data>  
  10.     </intent-filter>  
  11. </receiver>  


The summary is as follows:

For the following reasons:
(1) Actions corresponding to BOOT_COMPLETED and uses-permission are not added together
(2) The application is installed in the sd card. The application installed in the sd card can not receive BOOT_COMPLETED broadcasting.
(3) The system opens Fast Boot mode, in which the system boots without sending BOOT_COMPLETED broadcasting.
(4) The application has never been started again after installation. In this case, the application can not receive any broadcasting, including BOOT_COMPLETED, ACTION_PACKAGE_ADDED, CONNECTIVITY_ACTION, etc.
In order to enhance security control, the system is in a stopped state after the application is installed or the application management is forcibly shut down, in which no broadcasting can be received. It won't get out of this state until it's started (user opens or other application calls), so after Android 3.1
(1) The application cannot start itself after installation
(2) Programs without ui must be activated by other applications, such as its Activity, Service, Content Provider, which are called by other applications.


But why can't it sometimes be received?

1. After installing the application, start it once first.
2. If you sign, you can't install the apk file with eclipse. If you install it manually, you should start it again.
3. Add the following:
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <uses-permission android:name="android.permission.RESTART_PACKAGES" />
4. Add the following:

  1. <receiver android:name=".BootBroadcastReceiver" >  
  2.             <intent-filter>  
  3.                 <action android:name="android.intent.action.BOOT_COMPLETED" />  
  4.   
  5.                 <category android:name="android.intent.category.HOME" />  
  6.             </intent-filter>  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.PACKAGE_ADDED" />  
  9.                 <action android:name="android.intent.action.PACKAGE_REMOVED" />  
  10.                 <action android:name="android.intent.action.PACKAGE_REPLACED" />  
  11.   
  12.                 <data android:scheme="package" />  
  13.             </intent-filter>  
  14.         </receiver>  

5. Code section:
  1. public class BootBroadcastReceiver extends BroadcastReceiver  
  2. {  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent)  
  5.     {  
  6.         //Receiving broadcasting: running program after system start-up  
  7.         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))  
  8.         {  
  9.             Intent ootStartIntent = new Intent(context, Login_Activity.class);  
  10.             ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  11.             context.startActivity(ootStartIntent);  
  12.         }  
  13.         //Receive broadcasting: After installing and updating, start yourself automatically.         
  14.         if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) || intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED))  
  15.         {  
  16.             Intent ootStartIntent = new Intent(context, Login_Activity.class);  
  17.             ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  18.             context.startActivity(ootStartIntent);  
  19.         }  
  20.     }  
  21. }  


There is a final case:

Check whether there is a kind of software like 360 housekeeper in the system. They will shield some boot broadcasts by default, speed up the boot, and just turn it on.

Posted by bltesar on Sun, 24 Mar 2019 02:45:28 -0700