Java 不发出声音或振动的通知?
我正在制作一个程序,我希望应用程序发出通知。当通知关闭时,仅显示股票代码文本。没有声音、振动或光伴随着它 下面是我的代码示例:Java 不发出声音或振动的通知?,java,android,notifications,Java,Android,Notifications,我正在制作一个程序,我希望应用程序发出通知。当通知关闭时,仅显示股票代码文本。没有声音、振动或光伴随着它 下面是我的代码示例: int icon = R.drawable.icon; NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Context context = getApplicationContext();
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);
您是否确保设备没有静音,并且用户实际拾取了通知声音(也不是静音) 另一种尝试是:
[Note obj].sound = value
[Note obj].LEDARGB = value
[Note obj].vibrate = value
要使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"/>
要添加通知灯,您需要添加以下内容(注意.flags,而不是.defaults): 对于默认声音:
notification.defaults |= Notification.DEFAULT_SOUND;
对于默认振动模式:
notification.defaults |= Notification.DEFAULT_VIBRATE;
对于振动,正如Dean Thomas提到的,您需要一个权限
我发现在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\u SOUND
,default\u vibration
,default\u LIGHTS
。是的,声音打开,我在通知关闭时听到了一声叮当声。我将尝试代码,看看是否有效。我尝试了这个,并意识到这是我所做的振动,我的代码应该通知通知通知管理器使用默认的灯光和声音。。。所以它仍然不起作用
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());