android-我应该如何每天和凌晨12点使用Alarmmanager运行服务

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

解决方法:

您的问题是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>
上一篇:android-使用警报管理器启动服务


下一篇:PAT Basic Level 1029 旧键盘(find函数的使用)