2011-10-10 87 views
0

我使用以下代碼添加通知,並在通知中添加了兩個功能,一個是聲音,另一個是閃爍的燈光。通知聲音停止點擊

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()。

+0

嘿有人在android中使用通知,請給我一些建議。 –

+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; 
0

您可以創建NotificationCompat.Builder對象,而無需使用setSound()。這將創建一個沒有任何聲音的通知。

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