如何在设备启动后将数据加载到应用程序小部件
我正在尝试在设备启动后将数据读取到我的小部件。
我的启动接收器是这样的:
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;
}
}
我知道我得到了 BOOT_COMPLETED
意图(在小部件本身中使用它之后),但我只是无法读取我保存的数据。
我读到使用 SharedPreferences
是解决方案,但我知道当您启动设备时,SharedPreferences
不再存在。
我使用 Android SDK 中的内置 SQL 在内部保存数据。
请帮忙...:(
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... :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
BOOT_COMPLETED
广播时,外部存储可能尚未准备好。而且你的循环毫无意义。是的,SharedPreferences 在启动时就存在。
那么不清楚为什么您要等待外部存储,因为您的数据不在外部存储上。
然而,任何形式的 I/O 在启动时都可能花费太长的时间。让您的
BroadcastReceiver
在IntentService
上调用startService()
,该 IntentService 可以读取您的数据库或onHandleIntent 中的
并更新您的应用程序小部件。SharedPreferences
()External storage may not be ready by the time of a
BOOT_COMPLETED
broadcast. And your loops are pointless.Yes,
SharedPreferences
are 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
BroadcastReceiver
callstartService()
on anIntentService
that can read your database orSharedPreferences
inonHandleIntent()
and update your app widget.