There are several ways to do this, here is one:
Intent intent = new Intent(context, myService.class); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget); if (ismyserviceup(context)) { intent.setAction("STOP"); } PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.my_button, pendingIntent);
Then in the onStartCommand() you can check the intent action for "STOP" (maybe you should use the best line) and call stopSelf() .
source share