Please Help!. Android Widget Update Problem!!

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

Please Help!. Android Widget Update Problem!!

Postby manuel047 » Thu Apr 07, 2011 5:29 pm

Hello all, Please i would like your help in this as i have reached breaking point!! :( My problem is that am trying to update my widget periodically based on a user setting in the configuration class but its not working. if i set an initial update period, it works probably because OnUpdate is also called when you first start a widget (if am allowed to use that word) but if i try to set the update period from the configuration class, it never updates. here is what i have done so far. i am posting the parts that are relevant.

Widget Provider class onReceive() method :

Code: Select all
     public void onReceive(Context context, Intent intent){
       super.onReceive(context, intent);
       Log.d("OnReceive", "OnReceive called");
       String action = intent.getAction();
          Log.d("OnReceive", "OnReceive called twice");
          Bundle extras = intent.getExtras();
          if(extras != null){
          update_rate = extras.getLong("Refresh_Rate");
          int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
          AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        ComponentName thisAppWidget = new ComponentName(context.getPackageName(),WeatherWidgetProvider.class.getName() );
          int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

          //setAlarm(context, appWidgetId, update_rate);
          } else {

Widget Provider class onUpdate() method:

Code: Select all
   public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
       //super.onUpdate(context, appWidgetManager, appWidgetIds);
       long update_rate2 = 20000; // if i use this in place of update_rate it works, but its not from configuration class
        Log.d("OnUpdate", "OnUpdate called"); // stops here if i manually call from onRecieve()
       for (int appWidgetId : appWidgetIds) {
             setAlarm(context, appWidgetId, update_rate);

relevant part of Widget configuration class:

Code: Select all
int updateRate = convertToMillis(update.getSelectedItem().toString()); // get user settings for refresh update
             savePreferences(); // method to save preferences

      PendingIntent updatepending =   WeatherWidgetProvider.makeControlPendingIntent(self,WeatherService.UPDATE, appWidgetId);
                 try {
                 } catch (CanceledException e) {
                 Intent intent = new Intent(WeatherWidgetProvider.MY_WIDGET_UPDATE);
                 PendingIntent pending  = PendingIntent.getBroadcast(this, 0, intent, 0);
               AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
               alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+ updateRate, updateRate, pending);

             Intent resultValue = new Intent();
               setResult(RESULT_OK, resultValue);

SetAlarm and makecontrolpending method:
Code: Select all
public static PendingIntent makeControlPendingIntent(Context context, String command, int appWidgetId) {
      Intent active = new Intent(context,WeatherService.class);
       active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
       /*update test purpose*/
       active.putExtra("time", System.currentTimeMillis());
       /*this Uri data is to make the PendingIntent unique, so it wont be updated by FLAG_UPDATE_CURRENT
       so if there are multiple widget instances they wont override each other*/
       Uri data = Uri.withAppendedPath(Uri.parse("WeatherWidgetProvider://widget/id/#"+command+appWidgetId), String.valueOf(appWidgetId));
       return(PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT));

   public static void setAlarm(Context context, int appWidgetId, long updateRate) {
       PendingIntent newPending = makeControlPendingIntent(context,WeatherService.UPDATE,appWidgetId);
       AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
       if (updateRate > 0) {
           alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRate, newPending);
       } else {
           // on a negative updateRate stop the refreshing

i have also deliberately left the commented codes so you have an idea what i have also tried. I know am probably doing something wrong, but i have no idea anymore. i have been on this for some time now.. Thanks for taking a look and I highly appreciate all the help! Any other questions, please let me know.
Posts: 3
Joined: Tue Nov 23, 2010 1:21 pm


Re: Please Help!. Android Widget Update Problem!!

Postby manuel047 » Mon Apr 11, 2011 1:33 pm

anyone please that can help me :(
Posts: 3
Joined: Tue Nov 23, 2010 1:21 pm


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 5 guests