Я беру данные от пользователей и сохраняю в базу данных. Моя база данных принимает только одно значение. Таким образом, каждый раз, когда пользователь входит в базу данных, база данных обновляется. Я показываю эти данные в текстовом представлении AppWidget. Для этого я использую этот код:
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);
}
Мой виджет приложения должен обновляться после нажатия кнопки, а также когда пользователь вводит новые значения в базу данных во время выполнения. Но виджет приложения обновляется только после того, как приложение удалено с экрана, а затем установлено снова или программа перезапускается. Почему это происходит? Как обновить виджет моего приложения после нажатия кнопки ?????