我有一个带有 3个不同按钮的小部件,它包含了我从数据库中获取的一些值。
当在主应用程序中更新数据库时,我需要窗口小部件中的按钮从我的数据库中获取更新的值,然后显示它们。
我想在主应用程序中调用onStop
时在按钮上调用更新,但我无法弄清楚如何执行此操作
任何建议都将受到赞赏:)
@Override
protected void onStop()
{
//main application
.super.onStop();
WidgetProvider.updateMyWidgets(null, null); // does obviously not work as i dont have Context or Parcelable to send with it
dblose();
}
public static void updateMyWidget(Context context, Parcelable data)
{
AppWidgetManager man = AppWidgetManager.getInstance(context);
int[] ids = man.getAppWidgetIds( new ComponentName(context, WidgetProvider.class));
Intent updateIntent = new Intent();
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.putExtra(WidgetProvider.ACTION_WIDGET_BUTTON1, data);
updateIntent.putExtra(WidgetProvider.ACTION_WIDGET_BUTTON2, data);
updateIntent.putExtra(WidgetProvider.ACTION_WIDGET_BUTTON3, data);
context.sendBroadcast(updateIntent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
Log.w("no.reaving", "onUpdate method called");
getTimeFromDatabase();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent active = new Intent(context, WidgetProvider.class);
active.setAction(ACTION_WIDGET_BUTTON1);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 1, active, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btnTimerWidget1, actionPendingIntent);
active = new Intent(context, WidgetProvider.class);
active.setAction(ACTION_WIDGET_BUTTON2);
actionPendingIntent = PendingIntent.getBroadcast(context, 2, active, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btnTimerWidget2, actionPendingIntent);
active = new Intent(context, WidgetProvider.class);
active.setAction(ACTION_WIDGET_BUTTON3);
actionPendingIntent = PendingIntent.getBroadcast(context, 3, active, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btnTimerWidget3, actionPendingIntent);
// Set the text with the current time.
remoteViews.setTextViewText(R.id.btnTimerWidget1, TESTSTRING1 + " Hrs");
remoteViews.setTextViewText(R.id.btnTimerWidget2, TESTSTRING2 + " Hrs");
remoteViews.setTextViewText(R.id.btnTimerWidget3, TESTSTRING3 + " Hrs");
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent)
{
WidgetProvider.context = context;
db = new DatabaseHandler(context);
getTimeFromDatabase();
if (intent.getAction().equals(ACTION_WIDGET_BUTTON1)) {
newAlarm(TESTSTRING1);
showToast(TESTSTRING1);
} else if (intent.getAction().equals(ACTION_WIDGET_BUTTON2)) {
newAlarm(TESTSTRING2);
showToast(TESTSTRING2);
} else if (intent.getAction().equals(ACTION_WIDGET_BUTTON3)) {
newAlarm(TESTSTRING3);
showToast(TESTSTRING3);
} else {
super.onReceive(context, intent);
}
}
答案 0 :(得分:0)
AppWidgetProvider extends BroadcastReceiver所以当值更新时,您可以向AppWidgetProvider实现发送意图
修改强> 在您的主要应用程序中 而不是
WidgetProvider.updateMyWidgets(null, null);
您向WidgetProvider
发送视频广播,并在onReceive
方法中实施更新
如果可以帮助您,请参阅此project