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();
       
       if(MY_WIDGET_UPDATE.equals(action)){
          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);
                 
      this.onUpdate(context,appWidgetManager,appWidgetIds);
          } else {
         
          }
       }
       
    }


Widget Provider class onUpdate() method:

Code: Select all
  @Override
   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 {
                     updatepending.send();   
            
                 } catch (CanceledException e) {
                     e.printStackTrace();
                 }
                 
                 Intent intent = new Intent(WeatherWidgetProvider.MY_WIDGET_UPDATE);
                       intent.putExtra("Refresh_Rate",updateRate);
                 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();
               resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetId);
               setResult(RESULT_OK, resultValue);
               finish(); 



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.setAction(command);
       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));
       active.setData(data);
       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
           alarms.cancel(newPending);
       }
   }



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.
manuel047
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 23, 2010 1:21 pm

Top

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

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

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

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 18 guests