Argh! Timer was getting created over and over again, still if anyone can see other improvements please let me know.
On Mon, May 31, 2010 at 10:18 AM, Donal Rafferty <[email protected]> wrote: > Thanks all for the help. > > This is just a learning project for myself and wont be released on the > Android market. So I am aware that it may not be the best approach but it is > something to learn from. > > I have implemented the widget with the following code: > > > public class WlanWidget extends AppWidgetProvider{ > > RemoteViews remoteViews; > AppWidgetManager appWidgetManager; > ComponentName thisWidget; > WifiManager wifiManager; > > public void onUpdate(Context context, AppWidgetManager > appWidgetManager, > int[] appWidgetIds) { > Timer timer = new Timer(); > timer.scheduleAtFixedRate(new WlanTimer(context, > appWidgetManager), 1, 1000); > > > } > > > private class WlanTimer extends TimerTask{ > > RemoteViews remoteViews; > AppWidgetManager appWidgetManager; > ComponentName thisWidget; > > public WlanTimer(Context context, AppWidgetManager appWidgetManager) { > > this.appWidgetManager = appWidgetManager; > remoteViews = new RemoteViews(context.getPackageName(), > R.layout.widget); > thisWidget = new ComponentName(context, WlanWidget.class); > wifiManager = > (WifiManager)context.getSystemService(Context.WIFI_SERVICE); > > > } > > @Override > public void run() { > > remoteViews.setTextViewText(R.id.widget_textview, > wifiManager.getConnectionInfo().getSSID()); > if((GlobalData.MIN_RSSI_LEVEL - > (Math.abs(wifiManager.getConnectionInfo().getRssi())*2) <= 100)){ > remoteViews.setTextViewText(R.id.widget_percentage, > (GlobalData.MIN_RSSI_LEVEL - > (Math.abs(wifiManager.getConnectionInfo().getRssi())*2)+"%")); > } > else > { > remoteViews.setTextViewText(R.id.widget_percentage, > "100%"); > } > appWidgetManager.updateAppWidget(thisWidget, remoteViews); > } > > } > > } > > This appears to work fine but it then seems to make the > android.process.acore crash, it also crashes theandroid.process.acore if I > try to long press on the widget. > > I get the following messages in logcat then: > > 05-31 10:12:49.158: WARN/WindowManager(88): Key dispatching timed out > sending to com.android.launcher/com.android.launcher.Launcher > 05-31 10:12:49.168: WARN/WindowManager(88): Dispatch state: > {{KeyEvent{action=1 code=82 repeat=0 meta=0 scancode=139 mFlags=8} to > Window{433114a0 com.android.launcher/com.android.launcher.Launcher > paused=false} @ 1275296936375 lw=Window{433114a0 > com.android.launcher/com.android.launcher.Launcher paused=false} > lb=android.os.binderpr...@43311228 fin=false gfw=true ed=true tts=0 > wf=false fp=false mcf=Window{433114a0 > com.android.launcher/com.android.launcher.Launcher paused=false}}} > 05-31 10:12:49.168: WARN/WindowManager(88): Current state: {{null to > Window{433114a0 com.android.launcher/com.android.launcher.Launcher > paused=false} @ 1275297169174 lw=Window{433114a0 > com.android.launcher/com.android.launcher.Launcher paused=false} > lb=android.os.binderpr...@43311228 fin=false gfw=true ed=true tts=0 > wf=false fp=false mcf=Window{433114a0 > com.android.launcher/com.android.launcher.Launcher paused=false}}} > 05-31 10:12:49.238: INFO/ActivityManager(88): ANR in process: > android.process.acore (last in android.process.acore) > 05-31 10:12:49.238: INFO/ActivityManager(88): Annotation: > keyDispatchingTimedOut > 05-31 10:12:49.238: INFO/ActivityManager(88): CPU usage: > 05-31 10:12:49.238: INFO/ActivityManager(88): Load: 5.93 / 3.18 / 1.33 > 05-31 10:12:49.238: INFO/ActivityManager(88): CPU usage from 16159ms to > 59ms ago: > 05-31 10:12:49.238: INFO/ActivityManager(88): android.process.acore: 77% > = 77% user + 0% kernel / faults: 286 minor > 05-31 10:12:49.238: INFO/ActivityManager(88): system_server: 16% = 14% > user + 1% kernel / faults: 213 minor > 05-31 10:12:49.238: INFO/ActivityManager(88): com.networks: 4% = 4% user > + 0% kernel / faults: 365 minor > 05-31 10:12:49.238: INFO/ActivityManager(88): synaptics_wq: 0% = 0% user > + 0% kernel > 05-31 10:12:49.238: INFO/ActivityManager(88): wpa_supplicant: 0% = 0% > user + 0% kernel > 05-31 10:12:49.238: INFO/ActivityManager(88): tiwlan_wifi_wq: 0% = 0% > user + 0% kernel > 05-31 10:12:49.238: INFO/ActivityManager(88): events/0: 0% = 0% user + 0% > kernel > 05-31 10:12:49.238: INFO/ActivityManager(88): adbd: 0% = 0% user + 0% > kernel > 05-31 10:12:49.238: INFO/ActivityManager(88): TOTAL: 100% = 96% user + 3% > kernel > 05-31 10:12:49.248: INFO/ActivityManager(88): Removing old ANR trace file > from /data/anr/traces.txt > 05-31 10:12:49.288: INFO/Process(88): Sending signal. PID: 147 SIG: 3 > 05-31 10:12:49.288: INFO/dalvikvm(147): threadid=7: reacting to signal 3 > 05-31 10:12:49.488: INFO/Process(88): Sending signal. PID: 88 SIG: 3 > 05-31 10:12:49.488: INFO/dalvikvm(88): threadid=7: reacting to signal 3 > 05-31 10:12:49.578: INFO/dalvikvm(147): Wrote stack trace to > '/data/anr/traces.txt' > > Can anyone help with the problem? Or pin point the area the problem is > coming from? > > Thanks in advance > > > > On Sat, May 29, 2010 at 11:12 AM, Kostya Vasilyev <[email protected]>wrote: > >> You are welcome. >> >> It really is non-intuitive. I had reports from my users about widget >> intermittently not responding to clicks, but didn't know how to approach it. >> >> Finally, after buying a Moto Milestone, I saw that my widget got >> completely screwed up every time I pulled out the keyboard and the >> orientation changed. That gave me a good reproducible case to investigate. >> >> Anyway, good luck. >> >> -- Kostya >> >> 29.05.2010 14:09, Dirk Vranckaert пишет: >> >> Kostya, >>> >>> at first sight, that helped! >>> Thanks a lot for pointing me that out! >>> >>> Greetz.. >>> >>> Dirk >>> >>> On 29 mei, 11:04, Kostya Vasilyev<[email protected]> wrote: >>> >>> >>>> Dirk, >>>> >>>> This is the reason your widget sometimes stops responding to clicking. >>>> >>>> Each and every RemoteViews update needs to specify complete state, >>>> including data and PendingIntents (if any). The home screen process can >>>> be bumped out of memory, and Android uses the most recent RemoteViews to >>>> recreate widgets. If it's incomplete and only contains text updates, >>>> then pending intents will be lost. >>>> >>>> Same thing happens on orientation changes - there is no onUpdate(), just >>>> the mechanism described above. >>>> >>>> -- Kostya >>>> >>>> 2010/5/29 Dirk Vranckaert<[email protected]> >>>> >>>> >>>> >>>> >>>> >>>>> I think String is right about that. I wouldn't do that either just to >>>>> save battery life! >>>>> >>>>> >>>> >>>> >>>>> However I think it is better to set the update interval of the applet >>>>> to a high enough amount of time (like 24 hours or sth) and just update >>>>> the widget manually. I'm currently working on a widget to and in my >>>>> case the widget only receives an update upon clicking a certain >>>>> button. >>>>> >>>>> >>>> >>>> >>>>> The way I do that is like this: >>>>> >>>>> >>>> >>>> >>>>> RemoteViews rv = new RemoteViews(context.getPackageName(), WIDGET); >>>>> rv.setTextViewText(R.id.textField, "Updated value for some text on the >>>>> widget"); >>>>> ComponentName cn = new ComponentName(context, YourActivity.class); >>>>> AppWidgetManager mgr = AppWidgetManager.getInstance(context); >>>>> mgr.updateAppWidget(cn, rv); >>>>> >>>>> >>>> >>>> >>>>> That should basically work to update a widget. >>>>> Oh and one tip: using the this method you should do all updating just >>>>> after the line "rv.setTextViewText(...)". If you execute this entire >>>>> block a few times after each other you can get into troubles when >>>>> updating the widget. >>>>> >>>>> >>>> >>>> >>>>> Dirk >>>>> >>>>> >>>> >>>> >>>>> On 28 mei, 23:40, String<[email protected]> wrote: >>>>> >>>>> >>>>>> Before you get too far into this, you need to be aware that the widget >>>>>> architecture isn't designed to update that frequently. There's a lot >>>>>> of overhead, and updating every ten seconds will seriously impact >>>>>> battery life. As will having a service continually running in the >>>>>> background, for that matter. >>>>>> >>>>>> >>>>> >>>> >>>>> It's a cool idea but I'm not sure it's well suited for a widget, >>>>>> unfortunately. >>>>>> >>>>>> >>>>> >>>> >>>>> String >>>>>> >>>>>> >>>>> >>>> >>>>> On May 28, 2:43 pm, "[email protected]"<[email protected]> wrote: >>>>>> >>>>>> >>>>> >>>> >>>>> I am currently learning about widgets in Android. >>>>>>> >>>>>>> >>>>>> >>>> >>>>> I want to create a WIFI widget that will display the SSID, the RSSI >>>>>>> (Signal) level. >>>>>>> >>>>>>> >>>>>> >>>> >>>>> But I also want to be able to send it data from a service I am running >>>>>>> that calculates the Quality of Sound over wifi. >>>>>>> >>>>>>> >>>>>> >>>> >>>>> Here is what I have after some reading and a quick tutorial: >>>>>>> >>>>>>> >>>>>> >>>> >>>>> --- >>>>>>> >>>>>>> >>>>>> >>>> >>>>> public class WlanWidget extends AppWidgetProvider{ >>>>>>> >>>>>>> >>>>>> >>>> >>>>> RemoteViews remoteViews; >>>>>>> AppWidgetManager appWidgetManager; >>>>>>> ComponentName thisWidget; >>>>>>> WifiManager wifiManager; >>>>>>> >>>>>>> >>>>>> >>>> >>>>> public void onUpdate(Context context, AppWidgetManager >>>>>>> appWidgetManager, >>>>>>> int[] appWidgetIds) { >>>>>>> Timer timer = new Timer(); >>>>>>> timer.scheduleAtFixedRate(new >>>>>>> >>>>>>> >>>>>> WlanTimer(context, appWidgetManager), >>>>> >>>>> >>>>>> 1, 10000); >>>>>>> >>>>>>> >>>>>> >>>> >>>>> } >>>>>>> >>>>>>> >>>>>> >>>> >>>>> private class WlanTimer extends TimerTask{ >>>>>>> >>>>>>> >>>>>> >>>> >>>>> RemoteViews remoteViews; >>>>>>> AppWidgetManager appWidgetManager; >>>>>>> ComponentName thisWidget; >>>>>>> >>>>>>> >>>>>> >>>> >>>>> public WlanTimer(Context context, AppWidgetManager >>>>>>> >>>>>>> >>>>>> appWidgetManager) >>>>> >>>>> >>>>>> { >>>>>>> >>>>>>> >>>>>> >>>> >>>>> this.appWidgetManager = appWidgetManager; >>>>>>> remoteViews = new >>>>>>> >>>>>>> >>>>>> RemoteViews(context.getPackageName(), >>>>> >>>>> >>>>>> R.layout.widget); >>>>>>> thisWidget = new ComponentName(context, >>>>>>> >>>>>>> >>>>>> WlanWidget.class); >>>>> >>>>> >>>>>> wifiManager = >>>>>>> (WifiManager)context.getSystemService(Context.WIFI_SERVICE); >>>>>>> >>>>>>> >>>>>> >>>> >>>>> } >>>>>>> >>>>>>> >>>>>> >>>> >>>>> @Override >>>>>>> public void run() { >>>>>>> >>>>>>> >>>>>> >>>> >>>>> remoteViews.setTextViewText(R.id.widget_textview, >>>>> >>>>> >>>>>> wifiManager.getConnectionInfo().getSSID()); >>>>>>> appWidgetManager.updateAppWidget(thisWidget, >>>>>>> >>>>>>> >>>>>> remoteViews); >>>>> >>>>> >>>>>> } >>>>>>> >>>>>>> >>>>>> >>>> >>>>> } >>>>>>> >>>>>>> >>>>>> >>>> >>>>> --- >>>>>>> >>>>>>> >>>>>> >>>> >>>>> The above seems to work ok, it updates the SSID on the widget every 10 >>>>>>> seconds. >>>>>>> >>>>>>> >>>>>> >>>> >>>>> However what is the most efficent way to get the information from my >>>>>>> service that will be already running to update periodically on my >>>>>>> widget? >>>>>>> >>>>>>> >>>>>> >>>> >>>>> Also is there a better approach to updating the the widget rather than >>>>>>> using a timer and timertask? (Avoid polling) >>>>>>> >>>>>>> >>>>>> >>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Android Developers" group. >>>>> To post to this group, send email to >>>>> [email protected] >>>>> To unsubscribe from this group, send email to >>>>> [email protected]<android-developers%[email protected]><android-developers%2Bunsubs >>>>> [email protected]> >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/android-developers?hl=en >>>>> >>>>> >>>> >>> >> >> >> -- >> Kostya Vasilev -- WiFi Manager + pretty widget -- >> http://kmansoft.wordpress.com >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Android Developers" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected]<android-developers%[email protected]> >> For more options, visit this group at >> http://groups.google.com/group/android-developers?hl=en >> > > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

