java 找不到符号 NOTIFICATION_SERVICE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6843736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:32:55 来源:igfitidea点击:
Cannot find symbol NOTIFICATION_SERVICE?
提问by mrburns
package com.test.app;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class runOnBoot extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
}
When I try to build the package, it says
当我尝试构建包时,它说
compile:
[javac] Compiling 2 source files to /home/mrburns/Desktop/myapp/bin/classes
[javac] /home/mrburns/Desktop/myapp/src/com/test/app/runOnBoot.java:14: cannot find symbol
[javac] symbol : variable NOTIFICATION_SERVICE
[javac] location: class runOnBoot
[javac] NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
[javac] ^
[javac] 1 error
BUILD FAILED
回答by mcchots
I found calling this way works:
我发现这样调用有效:
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
回答by Shlublu
This should be Context.NOTIFICATION_SERVICE
:
这应该是Context.NOTIFICATION_SERVICE
:
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
回答by Luís Ledebour
NotificationManager mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
回答by Shah
You should better try this
你最好试试这个
NotificationManager nm = (NotificationManager)getSystemService(getApplicationContext().NOTIFICATION_SERVICE);