NOTIFICATION_RECEIVER无法解析为变量

时间:2013-12-03 23:49:56

标签: java android eclipse

我对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);
}

3 个答案:

答案 0 :(得分:3)

由于Fragment可以访问Activity,您可以getSystemService()

访问getActivity()
NotificationManager notificationManager 
       = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

因为ActivitygetSystemService()继承了Context

答案 1 :(得分:2)

NOTIFICATION_SERVICE不可见。尝试使用Context.NOTIFICATION_SERVICE;或

import static android.content.Context.NOTIFICATION_SERVICE;

答案 2 :(得分:1)

NOTIFICATION_SERVICEContext类的常量。

尝试

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