Android widget stops working after a while? -
i have flashlight app widget. widget used turn flashlight on , off , not display main activity or anything. after few hours, however, widget nothing. mean if click it, nothing happens. have 2 classes accomplish provider
, receiver
.
provider:
public class widgetprovider extends appwidgetprovider { @ override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { intent receiver = new intent(context, flashlightwidgetreceiver.class); receiver.setaction("com_flashlight"); receiver.putextra(appwidgetmanager.extra_appwidget_ids, appwidgetids); pendingintent pendingintent = pendingintent.getbroadcast(context, 0, receiver, 0); remoteviews views = new remoteviews(context.getpackagename(), r.layout.appwidget_layout); views.setonclickpendingintent(r.id.imagebutton, pendingintent); appwidgetmanager.updateappwidget(appwidgetids, views); } }
receiver:
public class flashlightwidgetreceiver extends broadcastreceiver { private static boolean islighton = false; private static camera camera; mediaplayer mp;@ override public void onreceive(context context, intent intent) { remoteviews views = new remoteviews(context.getpackagename(), r.layout.appwidget_layout); if (islighton) { views.setimageviewresource(r.id.imagebutton, r.drawable.btn_switch_off); mp = mediaplayer.create(context, r.raw.light_switch_off); } else { views.setimageviewresource(r.id.imagebutton, r.drawable.btn_switch_on); mp = mediaplayer.create(context, r.raw.light_switch_on); } mp.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @ override public void oncompletion(mediaplayer mp) { // todo auto-generated method stub mp.release(); } }); mp.start(); appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(context); appwidgetmanager.updateappwidget(new componentname(context, widgetprovider.class), views); if (islighton) { if (camera != null) { camera.stoppreview(); camera.release(); camera = null; islighton = false; } } else { camera = camera.open(); if (camera == null) { toast.maketext(context, "no camera!", toast.length_short).show(); } else { camera.parameters param = camera.getparameters(); param.setflashmode(camera.parameters.flash_mode_torch); try { camera.setparameters(param); camera.startpreview(); islighton = true; } catch (exception e) { toast.maketext(context, "no flash!", toast.length_short).show(); } } } } }
setup:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minwidth="40dp" android:minheight="40dp" android:maxwidth="40dp" android:maxheight="40dp" android:updateperiodmillis="86400000" android:initiallayout="@layout/appwidget_layout" android:resizemode="horizontal|vertical"> </appwidget-provider>
update: reducing update interval makes widget refresh more if it's stuck works again after 30 min , might freeze again sometime.
update 2: changing date instantly freezes widget until it's refreshed.
update 3: changing date somehow restarts launcher , whenever launcher restarted widget freezes 30 mins.
look post, think problem explained here the dark side of app widgets
Comments
Post a Comment