6
으로 Service
으로 시작하는 버튼으로 간단한 위젯을 만들려고합니다. 나는 그것을 잘 시작할 수 있지만 그것을 막을 수있는 방법을 알아낼 수 없다. (나는 BroadcastReceiver
또는 이와 비슷한 것으로 할 수 있지만 하드 코드는 피하고 싶다). 서비스의 onStartCommand()
에서비스를 시작하고 중지하는 PendingIntent
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);
그런 다음 "STOP"에 대한 의도 조치를 확인할 수 있습니다
이 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!!!!
}