简体   繁体   中英

Broadcast Receiver not receiving BOOT and MEDIA_MOUNTED intent

I'm trying to create a simple BroadcastReceiver that can receive the android.intent.action.BOOT_COMPLETED intent as well as the android.intent.action.MEDIA_MOUNTED intent. The idea is to start a service on receiving either of these intents. Thus the service should be started after Android boot is complete or when an USB storage device is connected to the Android target(if the service is not already started by then). Permissions used by the application are defined in this section

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

The following section defines the BroadcastReceiver responsible for handling the intents and starting the corresponding service.

<receiver
        android:name="com.example.systemupgradeapplication.IntentReceiver"
        android:label="USB Detection Receiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_REMOVED" />
            <action android:name="android.intent.action.MEDIA_EJECT" />
            <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <data android:scheme="file" />
        </intent-filter>
        <intent-filter android:priority="999" >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

The corresponding service that should be started from the Broadcast Receiver is defined in the following section

<service android:name=".SysUpgradeService" />

Note: The receiver and service components are defined within the <application> section of the Android Manifest.

The following snippet is the Class responsible for handling the intents broadcasted

public class IntentReceiver extends BroadcastReceiver {
private final static String TAG = "IntentReceiver";
private static boolean m_UsbInserted = false;
private static boolean m_UsbRemoved = true;
@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= ");
    // TODO Auto-generated method stub
    //if(intent.)
    String action = intent.getAction();
    if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
        String path = intent.getDataString();
        if(path.contains("usb")) {
            m_UsbInserted = true;
            Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= "+path);
            Intent myIntent = new Intent(context, SysUpgradeService.class);
            myIntent.putExtra("path", path);
            context.startService(myIntent);
        }

    }else if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
        Log.d(TAG, "BOOT Completed intent received");
context.startService(new Intent(context, SysUpgradeService.class));
    }

The problem I am facing is that none of the intents are arriving at my broadcast receiver(None of the Logs in the IntentReceiver class are being printed in logcat) even though I can see in the Android Debug logs that the BOOT_COMPLETE and MEDIA_MOUNTED intents are being broadcast. Also This application is not starting after android system is booting up.

I appreciate your help in this regard, what may be wrong with my approach and some possible solutions.

Okay, so I pushed the apk to /system/priv-app which is where System Applications which are part of custom ROM are placed. Now I do not need any activity in my application since it is part of the custom ROM and is recognized as a system application. It seems that if your application is a 3rd party application it must have an activity to be able to receive broadcasted intents. However in this case I have control over the custom ROM source code as well as root access on the device. So both the approaches work

  1. Make your application part of the custom ROM source, build and flash on device.
  2. Get root access on device, push your apk to /system/priv-app (4.4 onwards), reboot and voila!

Make Sure you have atleat one activity present in your Application.From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM