i'm trying launch service after clicking button in widget somehow service doesn't start. on internet , didn't found answer it.
this onupdate method
public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { // create intent launch service intent serviceintent = new intent(context, myservice.class); // pendingintent required onclickpendingintent // starts service button click pendingintent pendingserviceintent = pendingintent.getservice(context, 0, serviceintent, pendingintent.flag_update_current); // layout app widget , attach click listener // button remoteviews views = new remoteviews(context.getpackagename(), r.layout.meat); appwidgetmanager.updateappwidget(appwidgetids, views); log.i(tag, "onupdate on"); views.setonclickpendingintent(r.id.button, pendingserviceintent); context.startservice(serviceintent); super.onupdate(context, appwidgetmanager, appwidgetids); }
i tried onrecive method
@override public void onreceive(context context, intent intent) { intent intent1 = new intent (context, myservice.class); intent1.setflags (intent.flag_activity_new_task); context.startservice (intent1); super.onreceive(context, intent); }
i tried test toast message
public class myservice extends service { context context = getapplicationcontext(); @override public void oncreate() { toast.maketext(context, "the service started", toast.length_short).show(); }
this onstartcommand
@override public int onstartcommand(intent intent, int flags, int startid) { timer = new mycounter(21600000,1000); remoteviews v = new remoteviews(getpackagename(),r.layout.meat); v.settextviewtext(r.id.textview2, getcurrenttime()); appwidgetmanager.updateappwidget(thiswidget, v); timer.start(); toast.maketext(context, "the service started", toast.length_short).show(); appwidgetmanager.updateappwidget(thiswidget, v); return start_sticky; }
my manifest
<application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <receiver android:name=".meat" > <intent-filter> <action android:name="android.appwidget.action.appwidget_update" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/meat_info" /> </receiver> <service android:name=".myservice" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="action_widget_click_receiver"/> </intent-filter> </service> </application>
to start service have call startservice method in onreceive. replace line:
context.startactivity (intent1);
with this:
context.startservice(intent1);
and sure override onstartcommand method started service
Comments
Post a Comment