我对android很新。我通过教程完成了一些纯粹的活动。现在我想在片段中制作一些这些东西。
我修改了一些代码,但仍然有1个错误。 它位于底部:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
错误是我的主题标题。
出了什么问题?我该如何修复错误?
public void showNotification(){
String onderwerp = ("Herinnering");
String name = ("Vergeet uw medicijnen niet in te nemen!");
// Geluid notificatie
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Notificatie trigger
Intent intent = new Intent(getActivity(), NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
// De notificatie
Notification mNotification = new Notification.Builder(getActivity())
.setContentTitle(onderwerp)
.setContentText(name)
.setContentIntent(pIntent)
.setSound(soundUri)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotification.vibrate = new long[]{100, 200, 100, 500};
notificationManager.notify(0, mNotification);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
答案 0 :(得分:3)
由于Fragment
可以访问Activity
,您可以getSystemService()
getActivity()
NotificationManager notificationManager
= (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
因为Activity
从getSystemService()
继承了Context
。
答案 1 :(得分:2)
NOTIFICATION_SERVICE不可见。尝试使用Context.NOTIFICATION_SERVICE;或
import static android.content.Context.NOTIFICATION_SERVICE;
答案 2 :(得分:1)
NOTIFICATION_SERVICE
是Context
类的常量。
尝试
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);