我有一個小工具與3個不同的按鈕保存我從我的數據庫中獲得的一些值。在主應用程序中的小部件更新按鈕值
當數據庫在主應用程序中更新時,我需要小部件中的按鈕到從我的數據庫獲取更新後的值,然後顯示它們。
我想叫更新上的按鈕後onStop
是所謂的主要應用,但我只是無法弄清楚如何做到這一點。
任何suggesteons,將不勝感激:)
@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);
}
}
什麼錯誤的你好嗎? –
RuntimeException:無法停止活動MainOpeningActivity。我只是覺得必須有一個更好的方法,然後靜態地調用它,就像下面提到的廣播接收器一樣,但我不知道如何使用廣播接收器 – Reaver