I have setup the clickable event for the widget like below but the widget will not be clickable after like 7 or 8 hours.
This is the view wording_widget.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.MyWordingApp.AppWidget.Container"
android:id="@+id/widgetBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:theme="@style/Theme.MyWordingApp.AppWidgetContainer">
<TextView/>
<TextView/>
This is the widget logic:
public class WordingWidget extends AppWidgetProvider {
WordDBHelper dbHelper;
private static String UPDATE = "UPDATE";
private void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wording_widget);
updateWords(context, views);
views.setOnClickPendingIntent(R.id.widgetBox, getPendingSelfIntent(context, UPDATE));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (UPDATE.equals(intent.getAction())) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName watchWidget = new ComponentName(context, WordingWidget.class);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.wording_widget);
updateWords(context, remoteViews);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
private void updateWords(Context context, RemoteViews views) {
dbHelper = WordDBHelper.getInstance(context);
//get strings from db and update the views
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
}
Even though remoteView has been set the onlick event but why it stops working after a while? When I click the widget, onRecevie() is not called anymore.