onHandleIntent (의도) 내에서 일부 장기 실행 작업을 동 기적으로 수행하는 IntentService가 있습니다. 따라서 서비스가 실행되는 동안 알림을 표시하고 있습니다. 이제 사용자가 알림을 탭하면 서비스를 중지하고 싶습니다.보류 알림 수신 의도가 수신되지 않았습니다.
그래서 여기 있습니다. 서비스가 생성되면 등록 된 브로드 캐스트 - 수신기 클래스. 서비스가 작동 할 때 표시되는 보류중인 알림이있는 알림.
하지만 사용자가 알림을 탭하면 어떻게 든 방송 의도를 얻지 못합니다. 어떤 생각?
다음은 테스트를 위해 쉽게 다시 사용할 수있는 코드의 일부입니다.
public class SyncService extends IntentService
{
private static final String TAG = SyncService.class.getSimpleName();
private static final int NOTIFICATION_REF_ID = 1;
private static final String ACTION_CANCEL = TAG + ".CANCEL";
private CancelReceiver receiver;
public SyncService() {
super(SyncService.class.getSimpleName());
}
private class CancelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
stopSelf();
}
}
@Override
public void onCreate() {
super.onCreate();
receiver = new CancelReceiver();
IntentFilter filter = new IntentFilter(ACTION_CANCEL);
registerReceiver(receiver, filter);
}
@Override
public void onDestroy() {
super.onDestroy();
if (receiver != null)
unregisterReceiver(receiver);
}
@Override
protected void onHandleIntent(Intent intent) {
Intent cancelIntent = new Intent(this, CancelReceiver.class);
cancelIntent.setAction(ACTION_CANCEL);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
cancelIntent, 0);
// create notification and set pending-intent
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("some caption")
.setContentText("some hint")
.setTicker("some caption").setSmallIcon(R.drawable.ic_action_refresh)
.setOngoing(true).setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent).setProgress(0, 0, true);
Notification notification;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
notification = builder.getNotification();
else
notification = builder.build();
// show notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID,
notification);
// now do some long running work synchronously
// cancel/remove notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(SyncService.class.getCanonicalName(), NOTIFICATION_REF_ID);
}
}
이 서비스는 내 활동 내에서 시작됩니다 : 즉 this post에서 온다처럼
Intent syncIntent = new Intent(this, SyncService.class);
startService(syncIntent);
아이디어는 서비스를 중지 할 수 있습니다.
두 개의 알림 관리자를 사용하지 마십시오. – greenapps
사용자가 알림을 탭하면 사라 집니까? – greenapps
IntentService는 onHandleIntent() 작업이 완료되면 자동으로 stopSelf()를 호출합니다. onDestroy()가 브로드 캐스트 할 수있는 기회를 갖기 전에 수신자를 등록 취소하는지 궁금합니다. 나는 각 라이프 싸이클 메소드에 로그 문을 배치하여 물건이 호출되는 순서를 확인한다. –