[android-developers] Re: Weird java.util.Timer behavior in App Widget
Mark, I think you miss understood what I mean by a reference to my Widget. Unless I am thinking about this incorrectly. The function onReceive in the OnAlarmReceiver class needs to have a reference to the widget instance, see the below code. How else can the OnReceive function know how many times it has fired and change its firing rate accordingly? public class OnAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MyWidget myWidget = SomeMagicFunction(); // Check myWidget.count to see how many times this alarm has fired. if(myWidget.count == 60) { // We no longer want to fire once a second, we now want to fire once a minute. myWidget.alarmManager.cancel(myWidget.pi); myWidget.alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 1000 * 60, myWidget.pi); } } } I have two questions. 1) Is this the correct approach? If not then what. 2) If so then how do I do the following MyWidget myWidget = SomeMagicFunction() Kind regards, Cathal -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Weird java.util.Timer behavior in App Widget
Mark, thank you for your answer, especially on a Saturday. Let me explain exactly what I am trying to do. Perhaps it is against the SDKs ethos and that's why my simple task seems so difficult to implement using the Android SDK. I have an AppWidget with one button. When this button is pressed the AppWidget makes a call to a web service and displays the returned result on a label. This I have done and it works great. What I am currently trying to do is add a second label which displays the freshness of this data: seconds old, minutes old, hours old, etc. This would seem like a very common scenario. I can think of 5 AppWidgets --off the top of my head-- that I want to create that will require this exact same mechanism. Can you help me create this mechanism? You have already explained that a Timer and TimerTask is not the correct approach. It also seems like the AlarmManager is not going to work. You seem like an Android expert, can you help me achieve my goal? Cathal -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Weird java.util.Timer behavior in App Widget
Update your app widget's RemoteViews when the alarm goes off. Okay no problem. When the alarm period changes, have your alarm-handling logic cancel the existing alarm and schedule a new one. Okay but this is my question, how do I do this? My AppWidget's Button fires the below logic when it is pressed. AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(context, OnAlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); mgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 1000, pi); mgr.cancel(pi); There is no way to detect when the alarm period should change because there is no way to communicate between the Alarm's fired function and the AppWidgets non-existent state. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Weird java.util.Timer behavior in App Widget
Hey guys, I have an app widget which looks like this. public class MyWidget extends AppWidgetProvider { public static String ACTION_WIDGET_RECEIVER = ActionReceiverWidget; public Timer timer = new Timer(); public int count = 0; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); Intent active = new Intent(context, MyWidget.class); active.setAction(ACTION_WIDGET_RECEIVER); active.putExtra(msg, Message for Button 1); PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); remoteViews.setOnClickPendingIntent(R.id.button_one, actionPendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); } @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); final ComponentName thisWidget = new ComponentName(context, MyWidget.class.getName()); count = 0; timer.cancel(); timer.purge(); timer = new Timer(); timer.scheduleAtFixedRate(new UpdatedTimerTask(this, context, appWidgetManager), 0, 1000); } super.onReceive(context, intent); } } My UpdatedTimerTask looks like this. public class UpdatedTimerTask extends TimerTask { private Context context; private AppWidgetManager appWidgetManager; private RemoteViews remoteViews; private ComponentName thisWidget; private MyWidget widget; public UpdatedTimerTask(MyWidget widget, Context context, AppWidgetManager appWidgetManager) { this.widget = widget; this.context = context; this.appWidgetManager = appWidgetManager; this.remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); this.thisWidget = new ComponentName(context, MyWidget.class); } @Override public void run() { this.remoteViews.setTextViewText(R.id.label3, Count: + widget.count); this.appWidgetManager.updateAppWidget(this.thisWidget, this.remoteViews); widget.count ++; } } When I press Button1 on this widget its acts as I would expect. The count gets updated every second and displays in label3. Button1 Count: 1 Count: 2 Count: 3 etc However when I press Button1 again I expect the count to reset to 0 and continue from there. Whats actually happening is the two Timers are firing overlapped. Button1 Count: 1 Count: 2 Count: 3 Button1 Count: 1 Count: 4 Count: 2 etc Why aren't my calls to timer.cancel() and timer.purge() stopping the first timer before replacing it? How do I fix this issue??? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Weird java.util.Timer behavior in App Widget
Thanks Kostya but where should I store my objects? Is the correct approach to spawn a service and store my objects there? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Weird java.util.Timer behavior in App Widget
What I want to do is simple. I want my AppWidget to have a label which reads Updated: n seconds ago Updated: n minutes ago Updated: n hours ago Updated ages ago The labels text will depend on how long its been since a button on the widget was pressed. I am very conscious of battery power so I don't want a Timer or an Alarm to fire every second which checks how long its been. What I want is a Timer/Alarm that initially fires every second for 60 seconds after that it should only fire every minute for 60 minutes after that it should only fire every hour for 24 hours after this it should simply read Updated ages ago Can someone help me with this? Initially I tried to use a Timer to accomplish this, it worked great accept for the over lapping effect I explained above. I am now trying to use an AlarmManager but I don't know how to pass it a reference to my AppWidget instance. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Android Widget onActive()
Mark, You said that what I described --the android clock-- is not an app widget... then what is it? Perhaps I should be creating one of these and not an app widget. I need to know when a user is viewing my app widget (i.e. when my app widget is being drawn) surly there is a way to accomplish this using the android SDK???. Kind regards, Cathal -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Android Widget onActive()
Hi Guys, I've created a Widget and I would like to do something every time it is on screen. By on screen, I mean if the user moves to a screen that contains this widget. I want it to fire something like the below function. @override public void onActive(Context context, Intent intent) { // Your widget is visible, do something. } Is there a function like this in the Android SDK? The Android clock widgets seems to function like this, whenever the user moves to a screen that has an Andorid clock Widget, it updates it's display to show the current time. Kind regards, Cathal -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Null pointer after orientation change or back button pressed
Hi, I have written a simple application which displays GPS data on screen using Toasts. The important parts of my code are below. public class GPS extends Activity { private LocationManager lm; private LocationListener ll; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (savedInstanceState == null) { lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); ll = new MyLocationListener(); } button1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(first time button is pressed) lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll); else lm.removeUpdates(ll); } } } This application preforms perfectly until either the screens orientation is changed or the back button is pressed. If either of these happen then pressing the button causes a null pointer exception for lm. Why does changing the screens orientation or pressing the back button cause all variables defined at the Activity class level to be set to null? How do I fix this issue? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en