Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
API 21之前的Android后台服务_Android - Fatal编程技术网

API 21之前的Android后台服务

API 21之前的Android后台服务,android,Android,我试图在Android中执行一个周期性的后台计时器 下面的代码是从一个服务执行的,它与minSdk=21一起工作 @Override public int onStartCommand(Intent i, int flags, int startId) { Intent intent = new Intent(this, TestReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(t

我试图在Android中执行一个周期性的后台计时器

下面的代码是从一个服务执行的,它与minSdk=21一起工作

@Override
public int onStartCommand(Intent i, int flags, int startId) {   
    Intent intent = new Intent(this, TestReceiver.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, 60000, alarmIntent);
    return START_STICKY;
}
那听筒呢

@Override
public void onReceive(Context arg0, Intent arg1) {
    Log.i("TAG", "Tick: "+System.currentTimeMillis()+ " "+new Date());
}
但是如果我在MinSDK上尝试同样的方法,我建议使用API>19和

对于较新版本

重复方法不提供此类功能

别忘了使用WakefullBroadcastReceiver,我建议使用API>19和

对于较新版本

重复方法不提供此类功能


别忘了使用WakefullBroadcastReceiver

我在旧代码中看到的一种方法是在
onCreate
中调用
startCommand
。这两种方法都调用
onStartCommand
,但这样做似乎会使它“更粘”。几个月前,我提出了一个关于多余电话的问题,并得到了答复。我不能说这将解决您的特定问题,但可能值得一试。

我在旧代码中看到的一种方法是在
onCreate
的内部调用
startCommand
。这两种方法都调用
onStartCommand
,但这样做似乎会使它“更粘”。几个月前,我提出了一个关于多余电话的问题,并得到了答复。我不能说这将解决您的具体问题,但它可能值得一试