通知声音停止点击

时间:2011-10-10 05:10:17

标签: android

我使用以下代码添加通知,并在通知中添加两个功能,一个是声音,另一个是闪烁灯。

String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
                int icon = R.drawable.icon;
                CharSequence tickerText = "Hello";
                long when = System.currentTimeMillis();
                Notification notification = new Notification(icon, tickerText, when);
                RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.cutomnotification);
                contentView.setTextViewText(R.id.textView1,"Custom");
                notification.contentView = contentView;
                Intent notificationIntent = new Intent(v.getContext(), MyClass.class);
                PendingIntent contentIntent = PendingIntent.getActivity(v.getContext(), 0, notificationIntent, 0);
                notification.contentIntent = contentIntent;
                notification.sound=android.provider.Settings.System.DEFAULT_RINGTONE_URI;
                notification.ledARGB = 0xff00ff00;
                notification.ledOnMS = 300;
                notification.ledOffMS = 1000;
                notification.flags=Notification.FLAG_INSISTENT|Notification.FLAG_SHOW_LIGHTS|Notification.FLAG_NO_CLEAR;
                mNotificationManager.notify(HELLO_ID, notification);

在这里我也使用自定义布局

现在我有两个问题,

1)当用户点击通知标签时,声音会播放并停止播放,但我想继续播放声音,直到用户按下自定义布局上的任意按钮。

2)我在自定义布局中添加了一个按钮,现在我可以在其上实现onClickListener()。

2 个答案:

答案 0 :(得分:2)

notification.contentIntent = contentIntent;

之后添加此代码
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; 

答案 1 :(得分:0)

您可以在不使用 setSound()的情况下创建 NotificationCompat.Builder 对象。这将创建一个没有任何声音的通知。

 notification = mBuilder    
            .setStyle(notiStyle)   
            .setSmallIcon(notificationIcon)  
            .setTicker(title)  
            .setWhen(0)  
            .setAutoCancel(true)  
            .setContentTitle(title)  
            .setContentIntent(resultPendingIntent)  
            .build();