我想要实现的功能:
在特定日期,服务应该开始获取位置信息并将其推送到服务器。在某个特定日期,服务应该停止。
以下是我取得的成就:
我在尝试使该功能可靠且可用的过程中学到了什么:
试验:
我的问题:
设备的休眠模式是否会阻止服务正常运行? - 使用"融合位置"获取位置信息的含义,将位置缓存到数据库并将一些数据推送到服务器。
服务启动后,设备是否可以返回睡眠模式?
如果我选择在服务运行期间保持设备唤醒整个时间间隔,那么在服务的onCreate上获取唤醒锁并在onDestroy上发布它是一个好习惯吗?
< / LI> 醇>我的简化代码:
public class ServiceScheduler
{
//Class for scheduling a service from a scheduling instance (read from DB)
//.. much simple version just to explain
private void schedule(Scheduling scheduling){
//Intent for the ServiceStartBroadcastReceiver
Intent startServiceIntent = new Intent(ctx.getApplicationContext(), ServiceStartBroadcastReceiver.class);
//pending intent for the alarmManager
PendingIntent startPendingIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, startServiceIntent, 0);
//setting alarm to start the service at the specific time
//setExact's required API level is too high for the project
alarmManager.set(AlarmManager.RTC_WAKEUP, scheduling.startTime.getTime(), startPendingIntent);
//Intent for the ServiceStopBroadcastReceiver
Intent endServiceIntent = new Intent(ctx.getApplicationContext(), ServiceStopBroadcastReceiver.class);
//pending intent for the alarmManager
PendingIntent endPendingIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, endServiceIntent, 0);
//setting alarm to stop the service at the specific time
//setExact's required API level is too high for the project
alarmManager.set(AlarmManager.RTC_WAKEUP, scheduling.endTime.getTime(),endPendingIntent);
}
}
public class ServiceStartBroadcastReceiver extends WakefulBroadcastReceiver {
//BroadcastReceiver to start the service
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, GpsService.class);
//start in wakeful mode
startWakefulService(context, startServiceIntent);
}
}
public class ServiceStopBroadcastReceiver extends BroadcastReceiver {
//BroadcastReceiver to stop the service
@Override
public void onReceive(Context context, Intent intent) {
//Stopping the service
Intent stopServiceIntent = new Intent(context, GpsService.class);
context.stopService(stopServiceIntent);
}
}
public class GpsService extends Service {
//Using wake lock, is it necessary?
private PowerManager.WakeLock wakeLock;
@Override
public void onCreate() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "locationServiceWakeLock");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Wake lock !? is it necessary?
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
//completing the wakefull intent which is started at ServiceStartBroadcastReceiver
ServiceStartBroadcastReceiver.completeWakefulIntent(intent);
//Which mode should i use and why as return?
return START_REDELIVER_INTENT;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
//Wake lock !? is it necessary?
wakeLock.release();
}
}
请告诉我您的所有建议
我现在已经做了很长时间了,解决方案已经接近了。我已经阅读了我发现的所有文章和文档,但我仍然不确定。