在android中设置通知

发布于 2024-12-21 10:01:20 字数 495 浏览 1 评论 0原文

我正在尝试将通知设置为广播接收器类..当手机重新启动时..

代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-12-28 10:01:20

getSystemService 方法是 Context 类的成员。您似乎试图直接调用它而不引用 Context 对象,因此消息“The method getSystemService(String) is undefined for the type OnBootReceiver”。

将最后一行更改为

NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);

应该可以解决您的问题。

也许您的困惑源于这样一个事实:在 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

NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文