2012-01-12 48 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只要你想。