Android 使用报警管理器启动服务

Android 使用报警管理器启动服务,android,android-service,alarmmanager,Android,Android Service,Alarmmanager,我正在尝试使用Alarm Manager启动服务。我什么都试过了,只是还没有开始。我从活动的按钮侦听器调用下面提到的代码 private void setAlarmManager(String interval) throws NumberFormatException, IOException { AlarmManager service = (AlarmManager) getApplicationContext().getSystemService(Cont

我正在尝试使用Alarm Manager启动服务。我什么都试过了,只是还没有开始。我从活动的按钮侦听器调用下面提到的代码

    private void setAlarmManager(String interval) throws NumberFormatException, IOException
    {
        AlarmManager service = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(getApplicationContext(), MyService.class);

        PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(),
                                                           0,
                                                           i,
                                                           PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar cal = Calendar.getInstance();

        cal.add(Calendar.MINUTE, Integer.valueOf(interval));
        service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                                    (Integer.valueOf(interval) * 60000), pending);
    }
我的清单是这样的:

<service android:enabled="true" android:name="com.pack.android.service.MyService"
            />

pendingent.getBroadcast
更改为
pendingent.getService
,以使用
pendingent
启动服务,如下所示:

PendingIntent pending = PendingIntent.getService(getApplicationContext(),
                                     0,
                                     i,
                                   PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pending = PendingIntent.getService(getApplicationContext(),
                                     0,
                                     i,
                                   PendingIntent.FLAG_CANCEL_CURRENT);