错误:找不到符号方法getSystemService(Class <NotificationManager>)

我从google android开发者网站上获取了此代码,但不确定为什么会收到此错误。首先,我在Mainactivity中进行了此操作,并且工作正常,然后在AlarmReceiver.java中创建了另一个,但是发生了错误。

错误:找不到符号方法getSystemService(Class)

公共类AlarmReceiver扩展了BroadcastReceiver {     私有最终字符串CHANNEL_ID =“ Notification”;

@Override
public void onReceive(Context context,Intent intent) {

    createNotificationChannel();
    Intent notificationIntent = new Intent(context,Mapsactivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(Mapsactivity.class);
    stackBuilder.addNextIntent(notificationIntent);



    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.flaG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    Notification notification = builder.setContentTitle("Demo App Notification")
            .setContentText("New Notification From Demo App..")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();

    Notificationmanager notificationmanager = (Notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationmanager.notify(0,notification);
}
private void createNotificationChannel(){
    // Create the NotificationChannel,but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "NOTIFICATION";
        String description = "DESCRIPTION";
        int importance = Notificationmanager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        Notificationmanager notificationmanager = getSystemService(Notificationmanager.class);
        notificationmanager.createNotificationChannel(channel);
    }
}
}
longbp 回答:错误:找不到符号方法getSystemService(Class <NotificationManager>)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3143273.html

大家都在问