2012-01-12 75 views
5

我正在製作一個程序,我希望應用程序發出通知。當通知消失時,只顯示股票代碼文本。沒有聲音或振動或光線伴隨着它。通知沒有發出聲音或振動?

這裏是我的代碼示例:

int icon = R.drawable.icon; 
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

Context context = getApplicationContext();  

CharSequence contentTitle = "My notification"; 
CharSequence contentText = "Countdown Complete!"; 

Intent intent = new Intent(); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 
Notification notification = new Notification(icon, myCountDown.getName() + " is completed!", System.currentTimeMillis()); 

long[] vibrate = {0,100,200,300}; 
notification.vibrate = vibrate; 
notification.defaults |= Notification.DEFAULT_LIGHTS; 
notification.defaults |= Notification.DEFAULT_SOUND; 
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); 
notificationManager.notify(myCountDown.getId(), notification); 

回答

1

你確保該設備沒有靜音,實際上具有由用戶選擇了一個通知聲音(也並不沉默)?

另一件事,試圖將是:

[Note obj].sound = value 
[Note obj].LEDARGB = value 
[Note obj].vibrate = value 
+0

是的聲音是開着的,當通知消失時,我選擇了一個聲音。我會嘗試代碼,看看是否有用。 – Tj5 2012-01-19 17:56:56

+0

我試過這個,並意識到這是我至少爲振動做什麼,我的代碼應該通知通知管理器使用燈光和聲音的默認值...所以它仍然不起作用 – Tj5 2012-01-24 21:39:56

3

,使LED工作,你需要告訴它做什麼。

notification.ledOnMS = 1000; //Be on for a second 
    notification.ledOffMS = 1000; //Be off for a second 
    notification.ledARGB = Color.GREEN; //What colour should the LED be? 

爲了使振動的工作,你需要允許添加到您的清單

<uses-permission android:name="android.permission.VIBRATE"/> 
8

要添加通知燈,你需要添加這個(注意.flags,不.defaults):

notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
notification.ledARGB=0xffffffff; //color, in this case, white 
notification.ledOnMS=1000; //light on in milliseconds 
notification.ledOffMS=4000; //light off in milliseconds 

對於默認的聲音:

notification.defaults |= Notification.DEFAULT_SOUND; 

對於默認的振動模式:

notification.defaults |= Notification.DEFAULT_VIBRATE; 

振動,如迪安·托馬斯所說,你需要一個許可<uses-permission android:name="android.permission.VIBRATE"/>

0

我發現有在NotificationCompat.Builder一個setDefaults方法,它爲您提供了調整通知偏好。 下面是一個例子:

NotificationManager notificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

NotificationCompat.Builder builder = 
      new NotificationCompat.Builder(this); 

builder.setSmallIcon(R.drawable.taiwanpic) 
      .setWhen(System.currentTimeMillis()) 
      .setContentTitle("AAAAA") 
      .setContentText("BBBBB") 
      .setSubText("CCCCC") 
      .setContentInfo("DDDDD") 
      .setTicker("EEEEE") 
      .setDefaults(Notification.DEFAULT_ALL); 
int pid = android.os.Process.myPid(); 

notificationManager.notify(pid, builder.build()); 

如果你不使用的所有默認值,你也可以嘗試DEFAULT_SOUNDDEFAULT_VIBRATEDEFAULT_LIGHTS只要你想。