Abid
Abid

Reputation: 279

Android Widget click doesn't do anything after few hours

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.

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!

Broadcast receiver.

onUpdate

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);

    }

onReceive

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);
    }
}

onDelete

public void onDeleted(Context context, int[] appWidgetIds) {
    //  Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
        super.onDeleted(context, appWidgetIds);
    }

Service onstart where I am updating

    @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---> ");
        // 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);
 }

Upvotes: 3

Views: 1492

Answers (1)

GalDude33
GalDude33

Reputation: 7130

The problem is that you can't do a partiall update for a widget, you must set all the widget features, such as the set of PendingIntent's every time you push a new remoteView. (Partiall updates are only available for API14 and up...).

The reason your widgets are loosing their pendingIntents is that the android system saves the remoteView, and rebuilds your widget with it, in case it resets the widget (shortage of memmory, TaskManager/taskKiller in use, etc...), so you must set all the update code for the widget in the remoteView in your updateService. Otherwise, it's just won't set the pendingIntents again.

So just add the code setting the pendingIntents to the service and your problem will be solved =]

Upvotes: 3

Related Questions