0

I am trying to write a service that runs on phone boot, and must read data off the SD card. At first I was using a reciever for android.intent.action.BOOT_COMPLETED but switched to the intent below to make sure that the SD card has been loaded.

My Issue is that on a my Nexus 7, it doesn't appear to receive the MEDIA_MOUNTED intent. The Nexus 7 doesn't have an SD card (but it has separate SD card partition). I also tried the BOOT_COMPLETED intent, with the same luck. I have tested the same code on the emulator and my Thunderbolt, and both intents work.

Manifiest:

<receiver
    android:name=".StartupReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="Start the NFS Automounter Service">

    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED"></action>
        <data android:scheme="file"/> 
        <!-- <action android:name="android.intent.action.BOOT_COMPLETED"></action>-->
    </intent-filter>
</receiver>

The BroadcastReceiver class:

public class StartupReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) 
        //if ("android.intent.action.MEDIA_MOUNTED".equals(intent.getAction()))
        //{

            Log.d("NFS_Automounter", "Recieved Mount");
            Intent serviceIntent = new Intent("com.ancantus.nfsautomounter.AutomountService");
            context.startService(serviceIntent);
        //}
    }
}

I commented out the intent matching just to try and log if the class is executed at all.

My only hunch is that the Nexus 7 doesn't broadcast a MEDIA_MOUNTED because it doesn't have a real SD card; but I can't receive the BOOT_COMPLETED intent either.

And to forstall the question; yes I do have the BOOT_COMPLETED permission.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Ancantus
  • 688
  • 6
  • 18

2 Answers2

5

How many times must I type in this answer before it starts coming up in enough search results that people will find it? Maybe boldface caps will work:

STARTING WITH ANDROID 3.1, NO BroadcastReceiver WILL WORK, AT ALL, UNTIL SOMETHING HAS MANUALLY RUN ONE OF THE APPLICATION'S OTHER COMPONENTS, SUCH AS A USER RUNNING AN ACTIVITY.

This is in the documentation (albeit not well located), in blog posts, and in many StackOverflow answers, such as:

So, add an activity to your app. You need some activities anyway, for settings to control your background operation, for your documentation, for your license agreement, for your privacy policy, etc.

(note: I'm not really yelling at you -- I am just frustrated that this keeps coming up despite efforts to get the word out...)

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • No its quite alright, I am really surprised I didn't see anything in my searches (I was concentrating on the SD card issues rather than realizing `BroadcastReciever` itself wasn't operating). Your explanation makes perfect sense. I will write a activity tomorrow and see if it fixes the issue, but will probably accept Webnet's answer because my question was more focused on the SD card. – Ancantus Sep 04 '12 at 04:46
  • @Ancantus: I suspect that you will find that both receivers start working once you move the application out of its "stopped" state by running an activity. – CommonsWare Sep 04 '12 at 11:07
1

Please note that many Android devices emulate SD card in the way it does not affect access to the SD card even when desktop accesses it. Therefore it may be that Nexus 7 simply exposes all memory that way, so as it does not really mount anything, it'd not broadcast MEDIA_MOUNTED. If you want to do some tasks on boot, listening to BOOT_COMPLETED is the only correct approach.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Ok, so I was correct that the Nexus7 wouldn't broadcast the SD card mounting. So the SD card partition should be available when the `BOOT_COMPLETED` intent received. Is there any reason I wouldn't receive that intent? – Ancantus Sep 03 '12 at 23:33
  • I can't tell for sure as I got no Nexus here, but if there's no SD card to mount then it sounds like sufficient reason for not broadcasting `MEDIA_MOUNT` for me. – Marcin Orlowski Sep 03 '12 at 23:36
  • Is there any way to know if device will call BOOT_COMPLETED or not. – Hradesh Kumar Mar 12 '14 at 09:05
  • If you rebooted the device it will broadcast `BOOT_COMPLETED`. No need to check. – Marcin Orlowski Mar 12 '14 at 14:20