【问题标题】:How should I run a service daily and 12pm using alarmmanager我应该如何使用警报管理器每天和下午 12 点运行服务
【发布时间】:2012-01-25 14:48:48
【问题描述】:

我正在尝试每天晚上 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);
}

【问题讨论】:

  • 也帮我处理相关的android manifest reciever's code

标签: android notifications broadcastreceiver alarmmanager


【解决方案1】:

您的问题是 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2020-01-26
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多