1
我正在從用戶處獲取數據並存儲到數據庫中。我的數據庫只有一個值。所以每次用戶進入數據庫時,數據庫都會被更新。我顯示在AppWidget.For的TextView的這些金銀幣我使用此代碼:遠程查看更新問題
public void onUpdate(Context context, android.appwidget.AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//Get all ids
ComponentName thisWidget = new ComponentName(context.getPackageName(),SmsSchedulerWidget.class.getName());
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
//the datas to be shown are fetched from the database
DatabaseManager dbManager = new DatabaseManager(context);
dbManager.open();
contactNumber = dbManager.fetchContactNumber();
Log.i("contactNumber",contactNumber);
date = dbManager.fetchDate();
Log.i("date",date);
message = dbManager.fetchMessage();
Log.i("message",message);
status = dbManager.fetchStatus();
Log.i("status", status);
dbManager.closeDatabase();
//Build the intent to call the service
//it creates the UI for the given app widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.schdulesms_appwidget_layout);
remoteViews.setTextViewText(R.id.to_appwidget_saved_data,
contactNumber);
remoteViews.setTextViewText(R.id.date_appwidget_saved_data, date);
remoteViews.setTextViewText(R.id.status_appwidget_saved, status);
remoteViews.setTextViewText(R.id.message_appwidgset_saved_data,
message);
//to start the activity with the click on the layout
Intent clickIntent = new Intent(context.getApplicationContext(),MainActivity.class);
clickIntent.setFlags(clickIntent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent= PendingIntent.getActivity(context, 0, clickIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.full_widget, pendingIntent);
//to update the BroadCast Receiver so that it holds the current values in the layout
Intent updateIntent =new Intent(context,SmsSchedulerWidget.class);
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.setComponent(thisWidget);
PendingIntent pendingIntentForUpdate = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.logoBtn, pendingIntentForUpdate);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
我的應用程序部件應該按一下按鈕後更新本身以及用戶輸入新的值到在運行數據庫但appwidget只會在appwidget從屏幕上卸載並重新安裝或程序重新運行後才更新。爲什麼會發生這種情況?點擊按鈕後如何更新我的應用程序小部件?????
嗯,我不知道我是否正確,但擴展AppWidgetProvider的接收器顯示錯誤,當我嘗試覆蓋onReceive .....所以如果你可以通過演示或示例程序代碼詳細說明你的答案,那麼它會是我更容易實現它 – Prativa 2012-03-07 07:54:00
@prativa,這是一個很好的教程:http://www.vogella.de/articles/AndroidWidgets/article.html#updates。這裏使用的方法與我提出的有所不同,但它也應該起作用。 – Egor 2012-03-07 08:39:50
嗯,我發送廣播,並收到它在我的AppWidgetProvider的onReceive(),然後從onReceive()調用onUpdate()和它的工作很奇妙......但是這個過程創建了兩個AndroidAppWidget類的對象。 – Prativa 2012-03-09 05:04:37