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… 🙁
External storage may not be ready by the time of a
BOOT_COMPLETEDbroadcast. And your loops are pointless.Yes,
SharedPreferencesare there at boot time.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
BroadcastReceivercallstartService()on anIntentServicethat can read your database orSharedPreferencesinonHandleIntent()and update your app widget.