【问题标题】:Periodic location updates in a Service (Android)服务中的定期位置更新 (Android)
【发布时间】:2015-03-10 08:52:50
【问题描述】:

我正在尝试实现一个一旦启动就永远继续的服务(单元操作系统由于缺乏资源而强行关闭它)。该服务每五分钟获取一次用户的位置。在我的实现中,如果应用程序被发送到后台,服务就会工作但在应用程序终止时停止

您能否建议如何获得定期更新(例如 5 分钟后获取位置)

GeoFenceService.java

public class GeoFenceService extends Service {

private Context mContext;

private List<TestMalls> mTestMalls ;
private MyLocationManager mLocationManager;
private HashMap<Integer, Double> mallDistances;

private ServiceHandler mServiceHandler;
private Looper mServiceLooper;

@Override
public void onCreate(){
    super.onCreate();
    mContext = getBaseContext();

    this.mLocationManager = MyLocationManager.getInstance();
    HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startid){

    Message msg = mServiceHandler.obtainMessage();
    msg.arg1 = startid;
    mServiceHandler.sendMessage(msg);

    return START_STICKY;
}

private final class ServiceHandler extends Handler {

    public ServiceHandler(Looper looper){
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {

        Toast.makeText(mContext, "Service running ... ", Toast.LENGTH_SHORT).show();

        mLocationManager.startListenLocation(mContext);

        while (mLocationManager.hasLocation()) {

            for (TestMalls t : mTestMalls) {
                mallDistances.put(t.mallId, (double) mLocationManager.distanceInMetersFromLocation(t.latitude, t.longitude));
            }

            Iterator bt = mallDistances.entrySet().iterator();
            while (bt.hasNext()) {
                Map.Entry<Integer, Double> pair = (Map.Entry<Integer, Double>) bt.next();
                Log.v("Hash Map Distances", "Id: " + String.valueOf(pair.getKey()) + " Distance " + String.valueOf(pair.getValue()));
                bt.remove();
            }
        }
    }
}

@Override
public void onDestroy() {
    Log.v("Service", "done - finished");
}
}

【问题讨论】:

  • 你看到我的回答了吗?你在哪里接触到这个?

标签: android android-intent android-service android-pendingintent android-alarms


【解决方案1】:

使用getService() 方法创建一个PendingIntent 并将其添加到AlarmManager 以每5 分钟重复一次。确保Service 在完成其工作后自行停止(即最后调用stopSelf())。

Intent i = new Intent(getApplicationContext(), LocationService.class);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), requestCode, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, 0,
            5*60*1000, pi);

假设您的Service 工作正常,这将工作。

【讨论】:

  • 太棒了..谢谢..当应用程序终止时服务停止了怎么办
  • 一旦PendingIntent被添加到AlarmManager,它会在指定的时间间隔forever后周期性地启动Service,直到PendingIntent被移除AlarmManager 并取消。
猜你喜欢
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 2020-01-21
  • 2015-08-23
  • 1970-01-01
相关资源
最近更新 更多