你需要一个Service
和一个AlarmManager
。您的服务将处理获取位置并将其发布到服务器,AlarmManager
并将根据您决定的时间间隔调用您的服务。你应该在你想要的地方或其他地方用你Service
大致这样的方式初始化你的 AlarmManager :onCreate
AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, YourAlarmReceiver.class),PendingIntent.FLAG_CANCEL_CURRENT);
// Use inexact repeating which is easier on battery (system can phase events and not wake at exact times)
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, YOUR_ALARM_TRIGGER_AT_TIME,YOUR_ALARM_INTERVAL, pendingIntent);
YourAlarmReceiver 将启动您的服务
public class YourAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, YourService.class));
}
}
关于如何使用服务参考安卓网站http://developer.android.com/guide/topics/fundamentals/services.html