Android 创建通知时出现问题
我编写了两个通知函数。它们都在工作——只是旧版本使用了不推荐的方法。问题是我的新手机不会出现在菜单栏上,而且出现时也不会振动或发出噪音。我需要补充什么 旧版本:Android 创建通知时出现问题,android,android-notifications,Android,Android Notifications,我编写了两个通知函数。它们都在工作——只是旧版本使用了不推荐的方法。问题是我的新手机不会出现在菜单栏上,而且出现时也不会振动或发出噪音。我需要补充什么 旧版本: public void createNotification(String message) { Intent intent = new Intent(this, NotificationService.class); PendingIntent pi = PendingIntent.getActivit
public void createNotification(String message) {
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.logo_small, message,
System.currentTimeMillis());
n.setLatestEventInfo(this, message, message, pi);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(12327942 + notidchange, n);
++notidchange;
}
新版本:
public void createNotification(String message) {
//add diff icons and titles later
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentTitle(message)
.setContentText(message)
.setSmallIcon(R.drawable.logo_small)
.setAutoCancel(true) //do I want?
.setContentIntent(pi);
nm.notify(12327942 + notidchange++, builder.build());
}
在nm.notify之前,请输入此代码
Notification notif = builder.build();
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
notif.defaults = Notification.DEFAULT_ALL;
并替换nm.notify(12327942+notidchange++,builder.build())代码>
使用nm.notify(12327942+notidchange++,notif)代码>在此处添加您的menifest.xml文件