我正在尝试制作一个简单的小部件,其中包含一个按钮,可以使用OnClickPendingIntent()
启动一个Service
。 我可以成功启动它,但是我无法找到一种停止它的方法(我知道可以使用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!!!!
}