2013-06-28 34 views
0

我有一個小工具與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); 
    } 
} 
+0

什麼錯誤的你好嗎? –

+0

RuntimeException:無法停止活動MainOpeningActivity。我只是覺得必須有一個更好的方法,然後靜態地調用它,就像下面提到的廣播接收器一樣,但我不知道如何使用廣播接收器 – Reaver

回答

0

AppWidgetProvider extends BroadcastReceiver所以當值更新,您可以發送意圖您的AppWidgetProvider實現

編輯 在主應用程序,而不是 的

WidgetProvider.updateMyWidgets(null, null);

你在onReceive方法來發送broastcast您WidgetProvider和實施更新

看到這個project如果它可以幫助你

+0

請問您可以向我展示一些關於如何做的代碼那?我還沒有使用BroadcastReceiver,所以我沒有完全理解它 – Reaver

相關問題