Widget doesn't work after few hours

Put your problem here if it does not fit any of the other categories.

Widget doesn't work after few hours

Postby anaeem » Sun Jan 22, 2012 6:40 am

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


Code: Select all
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
Code: Select all
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

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




Service where I am updating textview

Code: Select all
@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);
}
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Top

Re: Widget doesn't work after few hours

Postby Phyll » Sun Jan 22, 2012 7:11 pm

Hi anaeem,

How are you registering your receiver? It sounds like your service is not persisting because it is not registered correctly. The OS kills it after some period of time and it cannot restart. Is it registered in the manifest?

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Widget doesn't work after few hours

Postby anaeem » Mon Jan 23, 2012 3:56 am

This is what someone told me and its working as well


"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 =]"

[url]http://stackoverflow.com/questions/8958126/android-widget-click-doesnt-do-anything-after-few-hours/8964762#8964762
[/url]
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Re: Widget doesn't work after few hours

Postby Phyll » Mon Jan 23, 2012 1:10 pm

Hi anaeem,

That's a pretty good answer to a frustrating problem.

Thanks,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests