在android中设置通知
我正在尝试将通知设置为广播接收器类..当手机重新启动时..
代码
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
aCtx=context.getApplicationContext();
String ns = aCtx.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
}
}
这是我收到此错误的
“The method getSystemService(String) is undefined for the type OnBootReceiver”任何人请帮忙:(
I am trying to set notification is a broadcast reciever class..when the phone reboots..
Here is the code
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
aCtx=context.getApplicationContext();
String ns = aCtx.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
}
}
I am getting this error "The method getSystemService(String) is undefined for the type OnBootReceiver"
Any one please help :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getSystemService 方法是 Context 类的成员。您似乎试图直接调用它而不引用 Context 对象,因此消息“The method getSystemService(String) is undefined for the type OnBootReceiver”。
将最后一行更改为
应该可以解决您的问题。
也许您的困惑源于这样一个事实:在 Activity 对象中,您只需调用 getSystemService 方法,它就可以在不引用任何对象的情况下工作。这是因为Activity类本身就是Context的子类。在这种情况下,调用 getSystemService() 而不引用任何对象是可行的,因为您调用的对象是 Context 对象。
The getSystemService method is a member of the Context class. You seem to be trying to call it directly without referencing a Context object hence the message "The method getSystemService(String) is undefined for the type OnBootReceiver".
Changing your last line to
should do the trick for you.
Perhaps your confusion stems from the fact within an Activity object you can simply call the getSystemService method and it works without referencing any object. This is because the Activity class itself is a subclass of Context. Calling getSystemService() without referencing any object works in this case because the object you are calling from is a Context object.