我正在尝试制作一个简单的小部件,其中带有一个启动按钮Service
与OnClickPendingIntent()
。我可以很好地启动它,但我无法找到阻止它的方法(我知道我可以用BroadcastReceiver
或类似的东西,但我想避免硬编码)。
这是我的代码:
Intent intent = new Intent(context, myService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);
if (!ismyserviceup(context)) {
views.setOnClickPendingIntent(R.id.my_button, pendingIntent);
} else {
// i need to stop it!!!!
}
有多种方法可以做到这一点,这里是一种:
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);
然后在服务上onStartCommand()
,您可以检查“STOP”的意图操作(您可能应该使用更好的字符串)并调用stopSelf()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)