2013-06-01 79 views
2

我正在嘗試使用操作按鈕創建一個android通知。我已經設置了一個開始一個活動,但我想另一個關閉通知,並沒有把用戶帶到另一個屏幕/活動(沒有采取我的應用程序的任何部分)Android:關閉通知的通知操作

我不想要使用'.auto cancel(true)',因爲我想要保留通知,除非按下此操作按鈕。

主要用途是針對正在進行的通知,因爲它們不能用滑動刪除。這個想法類似於「解僱」行動的android日曆。

+0

可以詳細一點?你的目標是什麼API級別?你應該能夠通過豆豆+通知來做到這一切。 – cbrulak

+0

我正在使用jellybean API - 我已經開始了一個活動,開始一個活動,購買我無法解決如何不開始一項活動。我可以使用廣播接收器,但我不知道如果這可以讓我清除通知。基本上我想通知行動清除自己(通知) – RiThBo

回答

3

創建通知時,將通知發送給廣播接收器。當通知操作被按下時,廣播接收器就會啓動,因此廣播接收器中的cancel()取消通知。

+7

你可以給這個確切的代碼,我無法按照你的指示。謝謝。 – ARK

1

IMO使用一個BroadcastReceiver是取消通知一個更清潔的方式:

在AndroidManifest.xml:

<receiver 
    android:name=.NotificationCancelReceiver" > 
    <intent-filter android:priority="999" > 
     <action android:name="com.example.cancel" /> 
    </intent-filter> 
</receiver> 

在Java文件

Intent cancel = new Intent("com.example.cancel"); 
PendingIntent cancelP = PendingIntent.getBroadcast(context, 0, cancel, PendingIntent.FLAG_CANCEL_CURRENT); 
NotificationCompat.Action actions[] = new NotificationCompat.Action[1]; 

NotificationCancelReceiver:

public class NotificationCancelReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //Cancel your ongoing Notification 
    } 
}