当我读取无论您在警报管理器中定义的时间段在sdk 21上面每隔1分钟发生一次。我正在运行一项服务,我希望我的方法执行每5分钟后。这是我的代码做了运行方式每隔5分钟在服务上面sdk 21
public class Servicebackground extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
System.out.println("service started in onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = newIntent(this,MyLocationListener.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 300000, pendingIntent);
System.out.println("service started in on create");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("service stopped");
}
@Override
public void onCreate() {
return;
}
public class MyLocationListener extends BroadcastReceiver implements LocationListener {
@Override
public void onReceive(Context context, Intent intent) {
Intent ii = new Intent(context, Servicebackground.class);
startService(ii);
}
在课堂上我打电话我已经做
startService(new Intent(this, Servicebackground.class));
但即使我已经定义的时间为5分钟,方法每一分钟后运行服务。如果你正在使用报警管理器,无论你什么时候定义该方法,每隔1分钟后,我会读到sdk 21之后。
请告诉我是否有任何错误。或者,请以任何其他方式向我建议,即使应用程序被杀死,并且每5分钟执行一次该方法,我也能够在后台运行该服务。如果还有其他可能的方法,请提出建议。
最好解释一下你的答案,而不是简单地在用户上转载一大堆未注释的代码。 – Kuffs
Y投票最新怎么了? – RDY
请解释你的代码。 –