android - What is making my widget's infinite loop stop working? -
my widget blank background switches between 2 colors 333ms delay between each switch. works fine @ first, after 158 switches, stops switching colors. thing is, don't rely on onupdate. everything's in infinite while-loop first called when put widget on home screen. causing stop switching after 158 switches? changing background color costly, , os disables widget?
colorswitchwidget.java:
public class colorswitchwidget extends appwidgetprovider { static void updateappwidget(context context, appwidgetmanager appwidgetmanager, int appwidgetid) { remoteviews views = new remoteviews(context.getpackagename(), r.layout.color_switch_widget); boolean lighton = true; while(true){ try{ thread.sleep(333); } catch(interruptedexception e){ } if (lighton) { views.setint(r.id.relativelayout1, "setbackgroundcolor", color.argb(150, 255, 248, 231)); //color 1 lighton = false; appwidgetmanager.updateappwidget(appwidgetid, views); } else { views.setint(r.id.relativelayout1, "setbackgroundcolor", color.argb(220, 255, 248, 231)); //color 2 lighton = true; appwidgetmanager.updateappwidget(appwidgetid, views); } } } @override public void onupdate(context context, appwidgetmanager, appwidgetmanager, int[] appwidgetids) { (int appwidgetid : appwidgetids) { updateappwidget(context, appwidgetmanager, appwidgetid); } } @override public void onenabled(context context) { } @override public void ondisabled(context context) { } }
color_switch_widget_info.xml:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialkeyguardlayout="@layout/color_switch_widget" android:initiallayout="@layout/color_switch_widget" android:minheight="40dp" android:minwidth="40dp" android:previewimage="@drawable/widget_icon_blink" android:resizemode="horizontal|vertical" android:updateperiodmillis="1800000" android:widgetcategory="home_screen"></appwidget-provider>
the widget works... not long. infinite loop poor choice?
edit: found workaround doesn't use thread.sleep(). i'm not sure if cause problems or memory issues, seems work now.
final handler myhandler = new handler(); final runnable blinkrunnable = new runnable(){ int lightoff = true; public void run(){ if(lightoff){ lightoff = false; views.setint(r.id.relativelayout1, "setbackgroundcolor", color.argb(220, 255, 248, 231)); //light "on" appwidgetmanager.updateappwidget(appwidgetid, views); myhandler.postdelayed(this, 333); } else{ lightoff = true; views.setint(r.id.relativelayout1, "setbackgroundcolor", color.argb(150, 255, 248, 231)); //light "off" appwidgetmanager.updateappwidget(appwidgetid, views); myhandler.postdelayed(this, 333); } } }; //start loop myhandler.post(blinkrunnable);
the problem here fact don't run on custom thread. method called main thread, , when pause main thread(thread.sleep()
) android thinks application isn't responding.
use new thread instead creating new class , letting extend thread
Comments
Post a Comment