我正在尝试使用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"
/>
我想按照用户指定的分钟数启动一个服务,但我的服务从未被调用。
如果我使用以下代码调用服务,它就能正常工作。我的项目目标版本是16。
getApplicationContext().startService(i);