想要在 OnReceive() 中创建通知时出错

发布于 2024-11-29 15:55:11 字数 1849 浏览 0 评论 0原文

我想显示一条通知并在每个周期重复一次。我阅读了如何制作该代码,并在扩展广播接收器的类中制作了该代码:

public class OnAlarmReceiver extends BroadcastReceiver{
    @Override
     public void onReceive(Context context, Intent intent) {


        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.icon;
        CharSequence tickerText = "Votre vidange approche";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);


        CharSequence contentTitle = "Notification";
        CharSequence contentText = "Vérifier votre kilométrage";
        Intent notificationIntent = new Intent(this, Acceuil.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);
     }

但是,我不知道为什么会出现此错误:

The method getSystemService(String) is undefined for the type OnAlarmReceiverThe constructor Intent(OnAlarmReceiver, Class<Acceuil>) is undefined
The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (OnAlarmReceiver, int, Intent, int)

在其他活动中,我想我必须这样做:

public void notifs() {
        AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i=new Intent(context, OnAlarmReceiver.class);
        PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);

        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi);
    }

这里有什么问题? 非常感谢。

I want to display a notification and repeat it every period. I read how to make that and i maked that code in the class that extends Broadcast Receiver:

public class OnAlarmReceiver extends BroadcastReceiver{
    @Override
     public void onReceive(Context context, Intent intent) {


        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.icon;
        CharSequence tickerText = "Votre vidange approche";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);


        CharSequence contentTitle = "Notification";
        CharSequence contentText = "Vérifier votre kilométrage";
        Intent notificationIntent = new Intent(this, Acceuil.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        final int HELLO_ID = 1;

        mNotificationManager.notify(HELLO_ID, notification);
     }

However, i don't know why i have this errors:

The method getSystemService(String) is undefined for the type OnAlarmReceiverThe constructor Intent(OnAlarmReceiver, Class<Acceuil>) is undefined
The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (OnAlarmReceiver, int, Intent, int)

In the other activity i guess that i have to do this:

public void notifs() {
        AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i=new Intent(context, OnAlarmReceiver.class);
        PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);

        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi);
    }

What's the problem here ?
Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遮了一弯 2024-12-06 15:55:11

区别在于:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

第二个之所以

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

有效,是因为您在应用程序上下文上调用 getSystemService ,而在第一个中,您尝试在 this 对象上调用它,该对象此时是 BroadcastReceiver 的子类的实例,它没有名为 getSystemService 的方法。

要解决此问题,请执行以下操作:

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


PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

此行会给您带来问题,因为您试图将自定义类的实例作为 t​​his 对象传递,而不是像它期望的那样传递 Context 对象。

要解决此问题,请执行以下操作:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

The difference is :

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

and

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

The second one works because you call getSystemService on an Application Context, where as in the first you are trying to call it on the this object which at the point is an instance of subclass of BroadcastReceiver which has no method called getSystemService.

To fix this do :

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


PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

This line gives you problems because you are trying to pass an instance of your custom class as the this object instead of a Context object like it expects.

To fix this do :

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