android - How to call function in Activity from HomeScreen Widget -
what proper method of calling function in application's activity
widget located on home screen? example, have background thread initialized in activity class, , able press widget home screen call specific functions start/stop thread without having enter application.
below stripped , barebone version of activity working with:
public class mainactivity extends appcompatactivity { private thread thread; private runnable runner; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); startthread(); } private void startthread() { //start/restart thread } public void stopthread() { //interupt thread } }
below barebone code home screen widget:
static void updateappwidget(context context, appwidgetmanager appwidgetmanager, int appwidgetid) { intent intent = new intent(context, mainactivity.class); intent.putextra("goal","stop"); pendingintent pendingintent = pendingintent.getactivity(context, 0, intent, 0); remoteviews views = new remoteviews(context.getpackagename(), r.layout.speed_widget); views.setonclickpendingintent(r.id.widgetbutton, pendingintent); appwidgetmanager.updateappwidget(appwidgetid, views); }
here current best solution is: research i've done far, seems need continue use putextra()
, getextra()
intent sent widget, , add in string whether call startthread()
or stopthread()
based on status of thread. current goal use this:
string intentgoal = getintent().getextras().getstring("goal"); if(intentgoal.compareto("stop")==0){ stopthread(); }else if(intentgoal.compareto("start")==0){ startthread(); }
and ran in overloaded version of onnewintent()
. however, intent sent widget results in call oncreate()
well, don't want have happen since restart thread , various other initialization code have re-ran well. tried setting launchmode singletask, still call oncreate()
rather onnewintent()
. recap, able press home screen widget, , have start or stop thread, or more call specific function in existing `mainactivity'.
i'm not expert in widget-building-process, assume need change:
pendingintent pendingintent = pendingintent.getactivity(context, 0, intent, 0);
to:
pendingintent pendingintent = pendingintent.getbroadcast(context, 0, intent, 0);
since need provide interaction application, don't need attach widget specific activity. instead of need use messaging mechanism. in android it's broadcastreciever
. example, use interact "start" service. can read here simple example.
also, according this answer came solution, need register reciever in manifest:
then in onrecieve
method can parse intent , whatever want. in case, recomend transport working background activity
service
, since activity can closed (okey, service
too, can restored , run without screen). , in mywidgetreciever.onreceive
need start service passing "stop" intent.
i'm not sure, but, can catch broadcast in service directly, not in external reciever, situation when service killed, not handle broadcast.
Comments
Post a Comment