0

I'm trying to read data to my widget after device boot.

My boot receiver is this:

    public class onBootReceiver extends BroadcastReceiver{

public static final String TAG = "BootReceiver";
private Context c;
@Override
public void onReceive(Context c, Intent i) {
    // TODO Auto-generated method stub

    boolean dontStop = true;
    while(dontStop)
    {
        try
        {   
            this.c=c;
            if(isExternalStorageMounted())
            {
                dontStop = false;
            }
            else
                for(int j=0;j<10000;j++)
                    Log.d(TAG, "###################### EXTERNAL STORAGE NOT MOUNTED ##########################");
        }
        catch (Exception e)
        {
            for(int j=0;j<10000;j++)
                Log.d(TAG, "###################### EXTERNAL STORAGE NOT MOUNTED ##########################");
        }
    }
    Intent externalStorageReady = new Intent(c, TheWidget.class);
    externalStorageReady.setAction(GlobalVars.WIDGET_INTENT_ACTION_READ_PREFS_AFTER_BOOT);
    c.sendBroadcast(externalStorageReady);

}
private boolean isExternalStorageMounted()
{
    String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_REMOVED.equals(state))
    {
        return false; 
    }
    else if (Environment.MEDIA_SHARED.equals(state))
    {
        return false; 
    }
    else if (Environment.MEDIA_UNMOUNTABLE.equals(state))
    {
        return false; 
    }
    else if (Environment.MEDIA_UNMOUNTED.equals(state))
    {
        return false; 
    }
    return true;
}

}

I know I get the BOOT_COMPLETED intent (after using it in the widget itself), but I just can't read my saved data.

I read that using SharedPreferences is the solution, but what I know is when you boot your device, the SharedPreferences is no longer there.

I save the data internally using built-in SQL in the Android SDK.

Please help... :(

César
  • 9,939
  • 6
  • 53
  • 74
Netanel
  • 11
  • 6

1 Answers1

0

External storage may not be ready by the time of a BOOT_COMPLETED broadcast. And your loops are pointless.

but what i know is when you boot your device, the SharedPreferences is no longer there.

Yes, SharedPreferences are there at boot time.

i save the data internally using built-in SQL in the android sdk.

Then it is unclear why you are waiting on external storage, since your data is not on external storage.

Any form of I/O may take too long, though, right at boot time. Have your BroadcastReceiver call startService() on an IntentService that can read your database or SharedPreferences in onHandleIntent() and update your app widget.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • some clarification needed: 1. the loops are for me to see in the command prompt because everything in the logcat is moving very fast, so don't pay attention to them... 2. do SharedPreferences save data even if i boot my device? 3. i am waiting for external storage because at least then i know that the internal must be mounted.... ill try to use SharedPreferences, if it won't work ill go towards service... – Netanel Dec 03 '11 at 00:56
  • @Netanel: "do SharedPreferences save data even if i boot my device?" -- yes. " i am waiting for external storage because at least then i know that the internal must be mounted" -- internal storage is mounted before the `BOOT_COMPLETED` broadcast is sent. – CommonsWare Dec 03 '11 at 01:27
  • ok, ill try to change my saved data to SharedPreferences, ill let you know if it worked... – Netanel Dec 03 '11 at 12:23
  • ok, so i changed my data to be saved using SharedPreferences, but now the problem is that i can't set the PendingIntent to the widget. i try to set the PendingIntent after i get a broadcast from my bootReceiver as mentioned above. i know these are a lot of questions and it seems like you are programming my app, but i'm new at this so please forgive me if i ask so many questions... Thanks :) – Netanel Dec 03 '11 at 23:23
  • @Netanel: I recommend that you ask a new StackOverflow question, with your current code and **specifically what is going wrong**. Saying stuff like "i can't set the PendingIntent to the widget" is not going to get you much help, just as telling a doctor "i don't feel good" is not going to get you much help. – CommonsWare Dec 04 '11 at 00:31