2013-06-01 142 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 
    } 
}