单击“addAction”图标时未取消Android通知

时间:2014-02-10 18:26:46

标签: android

我是Android的新手并开发了一个带通知的应用程序。

我需要用额外的按钮显示通知;当我点击它时,我需要在我的应用程序中进行一些活动。说“SettingsActivity”

我有以下代码。它工作正常,但按下addAction图标

时,通知不会自动取消

任何想法?
我的代码看起来像

Intent intent = new Intent(context, SettingsActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder =  
        new NotificationCompat.Builder(context)  
        .setSmallIcon(R.drawable.ic_launcher)  
        .setContentTitle("Notifications Example")  
        .setContentText("This is a test notification");  

builder.setDefaults(Notification.DEFAULT_ALL);

builder.setLights(Color.parseColor("#fec317"), 300, 1000);

Intent notificationIntent = new Intent(context, MainActivity.class);  
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,   
        PendingIntent.FLAG_UPDATE_CURRENT);  
builder.setContentIntent(contentIntent);  

builder.addAction(R.drawable.delete, "Call", pIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
manager.notify(0, builder.build());

2 个答案:

答案 0 :(得分:0)

作为解决方法:您可以使用mNotificationManager.cancel(id);手动删除它,其中id是您在创建此通知时传递的ID

答案 1 :(得分:0)

您可以制作一个侦听两个事件的广播接收器,一个用于启动通知,另一个用于停止通知。下面是我目前在我的一个项目中使用的一个示例。随意问你没有得到什么。

public class sendMessageBroadcast extends BroadcastReceiver {
    RemoteViews remoteViews;
    private static Thread thread;
    NotificationManager notificationManager;
    Notification notification;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("sendMessageBroadcast", "received");
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.customnotification);
        int SITUATION_ID = intent.getIntExtra("SITUATION_ID",-1);
        Intent remoteViewsIntent = new Intent("com.itcse.Situation.cancelNotification");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, remoteViewsIntent, PendingIntent.FLAG_UPDATE_CURRENT);//getActivity(getBaseContext(),0,intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if(intent.getAction().equals("com.itcse.Situation.cancelNotification"))  {
            thread.interrupt();
        }
        else {

            Log.d("Cancel Messge",""+ SITUATION_ID);

            if(Build.VERSION.SDK_INT <= 10 ){

                remoteViews.setViewVisibility(R.id.bCancel, View.GONE);
                remoteViews.setViewVisibility(R.id.tvCancelNotification, View.VISIBLE);
                notification = new Notification(R.drawable.situation,"Sending Situation",System.currentTimeMillis());
                notification.contentIntent = pendingIntent;
                notification.contentView = remoteViews;
                notification.flags = Notification.FLAG_AUTO_CANCEL;

                updateProgressBar(notification);
            }
            else {

                remoteViews.setOnClickPendingIntent(R.id.bCancel, pendingIntent);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                builder.setAutoCancel(true).setContent(remoteViews).setTicker("Sending Situation Message")
                        .setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable.situation);

                if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT <15){
                    updateProgressBar(builder.getNotification());
                }
                else if(Build.VERSION.SDK_INT >=15){
                    updateProgressBar(builder.build());
                }
            }
        }
    }

    public void updateProgressBar(final Notification notification){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //unregisterReceiver(broadcastReceiver);
                int incr;
                boolean send=true;
                for(incr = 0;incr<=100;incr+=10){
                    remoteViews.setProgressBar(R.id.progressBar, 100, incr, false);
                    notificationManager.notify(1,notification);
                    if(Thread.currentThread().isInterrupted()){
                        Log.d("Thread", "Interrupted");
                        send=false;
                        break;
                    }

                    try{
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e){
                        Log.d("Thread", "Thread interrupted while sleeping");
                        send = false;
                        break;
                    }
                }
                Log.d("Thread","Value of incr "+incr);
                remoteViews.setViewVisibility(R.id.bCancel, View.GONE);
                remoteViews.setViewVisibility(R.id.tvCancelNotification, View.GONE);
                remoteViews.setViewVisibility(R.id.progressBar, View.GONE);
                if(send) {
                    sendMessage();
                    remoteViews.setTextViewText(R.id.tvNotificationHeading, "Message send");
                }
                else {
                    remoteViews.setTextViewText(R.id.tvNotificationHeading, "Message not send");
                    notification.tickerText = "Message not send";
                }
                notificationManager.notify(1, notification);
            }
        });
        thread.start();
    }

    private void sendMessage() {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("5556",null,"3G",null,null);
    }
}