0

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); 

아이디어는 서비스를 중지 할 수 있습니다.

+0

두 개의 알림 관리자를 사용하지 마십시오. – greenapps

+0

사용자가 알림을 탭하면 사라 집니까? – greenapps

+0

IntentService는 onHandleIntent() 작업이 완료되면 자동으로 stopSelf()를 호출합니다. onDestroy()가 브로드 캐스트 할 수있는 기회를 갖기 전에 수신자를 등록 취소하는지 궁금합니다. 나는 각 라이프 싸이클 메소드에 로그 문을 배치하여 물건이 호출되는 순서를 확인한다. –

답변

0

변경

Intent cancelIntent = new Intent(this, CancelReceiver.class); 

onReceive()이 완료 될 때까지 계획된 작업이 계속됩니다 onDestroy() 호출 stopSelf()를 호출하는 호출됩니다 있지만

Intent cancelIntent = new Intent(); 

합니다. 이 '동 기적으로 일부 긴 실행 작업'onReceive에 당신의 동기화 클래스에 true로

private boolean cancelled = false; 

세트를 추가 처리하기 위해 정기적으로 cancelled을 확인하고 탈옥.

+0

의도가 생성자에서 변경된 이유는 무엇입니까? 조금 자세히 설명해 주시겠습니까? – Matthias

+0

의견을 보내주십시오. 변경된 의도를 시도 했습니까? 작동합니까? – greenapps

+0

나는 사무실에 없다. 그러나 변경된 코드를 테스트하자 마자 피드백을 보내 드리겠습니다. – Matthias