the onupdate method of widget should 2 things:
- set onclicklistener opens activity when click widget.
- set alarmmanager updates textboxes on widget every 10 seconds.
this how method looks:
@override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { // there may multiple widgets active, update of them final int n = appwidgetids.length; (int = 0; < n; i++) { // create intent launch activity intent openapp = new intent(context, mainactivity.class); openapp.putextra("intentid", 1); pendingintent pendingappintent = pendingintent.getactivity(context, 1, openapp, 0); remoteviews views = new remoteviews(context.getpackagename(), r.layout.hsvuhr_widget); views.setonclickpendingintent(r.id.rlmy, pendingappintent); intent update = new intent(context, updateservice.class); update.putextra("intentid",2); pendingintent pendingupdateintent = pendingintent.getservice(context, 2, update, 0); alarmmanager alarm = (alarmmanager) context.getsystemservice(context.alarm_service); alarm.cancel(pendingupdateintent); long interval = 10000; alarm.setrepeating(alarmmanager.elapsed_realtime, systemclock.elapsedrealtime(),interval, pendingupdateintent); } }
but in code mistake. onclicklistener not work. widget updates every 10 seconds, nothing opens when click widget.
can see mistake?
edit: how changed code. here appwidgetprovider
@override public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { // create intent launch activity intent openapp = new intent(context, mainactivity.class); openapp.putextra("intentid", 1); pendingintent pendingappintent = pendingintent.getactivity(context, 1, openapp, 0); remoteviews views = new remoteviews(context.getpackagename(), r.layout.hsvuhr_widget); views.setonclickpendingintent(r.id.rlmy, pendingappintent); intent update = new intent(context, updateservice.class); update.putextra("intentid",2); pendingintent pendingupdateintent = pendingintent.getservice(context, 2, update, 0); alarmmanager alarm = (alarmmanager) context.getsystemservice(context.alarm_service); alarm.cancel(pendingupdateintent); long interval = 5000; alarm.setrepeating(alarmmanager.elapsed_realtime, systemclock.elapsedrealtime(),interval, pendingupdateintent); appwidgetmanager.updateappwidget(appwidgetids, views); }
and here updateclass:
@override protected void onhandleintent(intent intent) { context context = this; appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(context); remoteviews remoteviews = new remoteviews(context.getpackagename(), r.layout.hsvuhr_widget); componentname thiswidget = new componentname(context,hsvuhrwidget.class); remoteviews.settextviewtext("stuff"); appwidgetmanager.updateappwidget(thiswidget, remoteviews); }
so onupdate call updateclass every 5 seconds. update class fire appwidgetmanager.updateappwidget(thiswidget, remoteviews); , after that, in appwidgetprovider there appwidgetmanager.updateappwidget(appwidgetids, views).
problem now: widget updates correctly, after first update onclickevent not fire. can open app @ first after update can't anymore. mistake?
at end of method call
appwidgetmanager.updateappwidget(appwidgetids, remoteviews);
Comments
Post a Comment