我应该如何使用 Alarmmanager 每天和中午 12 点运行服务

发布于 2024-12-29 02:24:08 字数 948 浏览 2 评论 0原文

我试图每天晚上 7 点打开一项新服务,该服务会在启动时通知我,但我无法解决这个问题,也不明白为什么。任何人都可以帮助我,谢谢 这是代码


这是我在 DaysCounterActivity 类中编写的代码

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 05);
    calendar.set(Calendar.SECOND, 00);


    Intent intent = new Intent(this, MyReciever.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);


    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pintent);

这是 MyReviever 类 onRevcieve 方法

    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.saaram.MyService");
    context.startService(serviceIntent);
}

I am trying to open a new service daily at 7 pm that would notify me on startup but i cant resolve this problem and cant understand why. can anyone help me out thankx Here is the code


Here is the code i wrote in the DaysCounterActivity class

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 19);
    calendar.set(Calendar.MINUTE, 05);
    calendar.set(Calendar.SECOND, 00);


    Intent intent = new Intent(this, MyReciever.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);


    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pintent);

and here is MyReviever class onRevcieve method

    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    Intent serviceIntent = new Intent();
    serviceIntent.setAction("com.saaram.MyService");
    context.startService(serviceIntent);
}

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

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

发布评论

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

评论(1

另类 2025-01-05 02:24:09

您的问题是 pintent 正在尝试运行服务,但 MyReceiver 是广播接收器。如果您将 MyReceiver 更改为服务,它就会起作用。

public class MyReceiver extends Service {


    public int onStartCommand(Intent intent, int flags, int startId) {
        //Anything you put here is run at the time you set the alarm to
        }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
        }

    }

在你的清单中你只需这样声明:

<service android:name=".MyReceiver"></service>

Your problem is that pintent is trying to run a Service, but MyReceiver is a Broadcast Receiver. It would work if you changed MyReceiver to a Service.

public class MyReceiver extends Service {


    public int onStartCommand(Intent intent, int flags, int startId) {
        //Anything you put here is run at the time you set the alarm to
        }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
        }

    }

In your manifest you would just declare it like so:

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