Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Widget click doesn't do anything after few hours
    text
    copied!<p>I have a widget that has a refresh button and a textview. Refresh updates the content and when user clicks on textview it starts a new activity.</p> <p>Problem is it works fine for a few hours and then onclick and refresh button doesn't do anything. Nothing is captured in logcat. Also If user deletes widget and put a new one it starts working for a few hours and then the same story :(...what am I doing wrong!</p> <p>Broadcast receiver.</p> <p>onUpdate</p> <pre><code>public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { long interval = getrefresInterval(); final Intent intent = new Intent(context, UpdateService.class); final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0); final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm.cancel(pending); alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending); // Build the intent to call the service RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget); // To react to a click we have to use a pending intent as the // onClickListener is excecuted by the homescreen application Intent ClickIntent = new Intent(context.getApplicationContext(),widgetHadith.class); Intent UpdateIntent = new Intent(context.getApplicationContext(),UpdateService.class); PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, ClickIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pendingIntentUpdate = PendingIntent.getService(context.getApplicationContext(), 0, UpdateIntent, PendingIntent.FLAG_UPDATE_CURRENT); //use this to update text on widget. if use this put UpdateService.class to intent remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent); remoteViews.setOnClickPendingIntent(R.id.widget_refresh, pendingIntentUpdate); // Finally update all widgets with the information about the click listener appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); // Update the widgets via the service context.startService(intent); } </code></pre> <p>onReceive</p> <pre><code>public void onReceive(Context context, Intent intent) { // v1.5 fix that doesn't call onDelete Action final String action = intent.getAction(); if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { final int appWidgetId = intent.getExtras().getInt( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { this.onDeleted(context, new int[] { appWidgetId }); } } else { super.onReceive(context, intent); } } </code></pre> <p>onDelete</p> <pre><code>public void onDeleted(Context context, int[] appWidgetIds) { // Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show(); super.onDeleted(context, appWidgetIds); } </code></pre> <p>Service onstart where I am updating</p> <pre><code> @Override public void onStart(Intent intent, int startId) { RemoteViews updateViews = new RemoteViews(this.getPackageName(),R.layout.widget); processDatabase(); Spanned text = LoadHadith(); String hadith = text.toString(); Log.d("BR", "service---&gt; "); // set the text of component TextView with id 'message' updateViews.setTextViewText(R.id.widget_textview, text); //Push update for this widget to the home screen ComponentName thisWidget = new ComponentName(this, HelloWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, updateViews); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload