2011-09-19 1 views
14

알림이 지워지는시기를 감지하려고합니다. 내 질문에 직접 언급 한이 내용은 answer입니다.Notification.deleteIntent 사용 방법

// usual Notification initialization here 
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0); 
notificationManager.notify(123, notification) 

이것은 CleanUpIntent 클래스입니다 : 이것은 내가 조치 구현하고있어 어떻게 난 그저 내가 평소하지만 같은 알림을 시작, 이후

class CleanUpIntent extends IntentService { 
    public CleanUpIntent() { 
     super("CleanUpIntent"); 
    } 

    @Override 
    protected void onHandleIntent(Intent arg0) { 
     // clean up code 
    } 
} 

을 나는 (그것을 테스트에 갈 때 "모든 알림 지우기"를 누르면) 아무 일도 일어나지 않습니다. IntentService가 시작될 때 LogCat에 인쇄 할 코드 줄을 삽입했지만 아무 것도 실행하지 않았습니다. 이것이 내가 Notification.deleteIntent를 사용하는 방법입니까?

답변

3

은 무엇 당신이해야 할 것은 BroadcastReceiver (아마 ServiceregisterReceiver를 사용하여의 AndroidManifest.xml에서 또는 대안)를 등록하고 해당 수신기에 의해 잡힐 것 IntentdeleteIntent을 설정합니다.

+0

IntentFilter 플래그가 알림을 지울 때 잡기 위해 무엇인지 알고 있습니까? – Brian

+0

'IntentFilter' 플래그가 아니며,'BroadcastReceiver'입니다 : http://code.google.com/p/islamictools/source/browse/trunk/IslamicTools/src/com/alpha/commun/MsgNotification을보십시오. .java? spec = svn11 & r = 11 당신이 어떻게 할 것인지 예를 들어 보자. – Femi

+0

나는 BroadcastReceivers에 너무 친숙하지 않지만 내 질문은 수신기를 등록하는 방법에 관한 것이었다. Content.registerReceiver를 호출하거나 수동으로 AndroidManifest에 넣을 지 여부에 상관없이 IntentFilter를 제공 할 필요가 없습니까? – Brian

0

getService 대신 getBroadcast methode를 사용해야하며 특정 Action에 대해 수신자를 등록해야합니다.

38

사용자가 알림을 삭제할 때마다 호출되는 샘플 코드가 도움이되기를 바랍니다.

.... 
notificationBuilder.setDeleteIntent(getDeleteIntent()); 
.... 


protected PendingIntent getDeleteIntent() 
{ 
    Intent intent = new Intent(mContext, NotificationBroadcastReceiver.class); 
    intent.setAction("notification_cancelled"); 
    return PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
} 

NotificationBroadcastReceiver.java

@Override 
    public void onReceive(Context context, Intent intent) 
    { 
     String action = intent.getAction(); 
     if(action.equals("notification_cancelled")) 
     { 
      // your code 
     } 
    } 

AndroidManifiest.xml

<receiver android:name=".NotificationBroadcastReceiver"> 
       <intent-filter> 
        <action android:name="notification_cancelled"/> 
       </intent-filter> 
      </receiver> 
+4

'action' 동등성을 절대적으로 확인할 필요가 없습니다. –

+7

애플리케이션은 알림 내용이나 다른 조건에 따라 동일한 리시버로 다른 알림을 생성하거나 원하는 다른 작업을 생성 할 수 있습니다. 이 경우, 조치를 정의 할 수 있습니다. – MeanderingCode

+1

이 코드는 모든 알림에 대해 PendingIntent를이 방식으로 재사용한다는 점에 유의하십시오. Intent.isEqual()에 의해 검사 된 다른 요청 코드 나 다른 데이터를 사용하여 unqiue를 생성하는 방법에 대한 설명을 읽으십시오. – althaus

-4

명시 적 수신기가 필요하지 않습니다. clear 버튼을 누르는 동안 deleteIntent가 자동으로 호출됩니다.