具有位置侦听器的Android后台服务

时间:2018-11-28 11:34:50

标签: android service locationlistener

所以我有一个包含位置侦听器的后台服务,我期望的是当我按下主页按钮时,由于位置已更改,因此应该调用位置侦听器,并在日志中打印一条消息,所以我知道它仍然存在听位置更改,但是发生的是:

-如果我处于应用程序活动中,然后按了主页按钮,然后更改了位置,则在日志中看到一条消息,确认收听者注意到位置已更改,但是当我再次更改位置时,它会就像侦听器处于暂停状态一样,因此任何尝试更改位置的尝试都不会调用侦听器,并且我在日志中也看不到任何消息,这是在第一次调用,第二次,第三次等等位置之后发生的监听器什么也没做。

返回到应用程序主活动,除非再次按下主屏幕按钮,否则实际上它将一直被再次调用并且没有任何问题。

这是我的服务等级

package com.example.husseinjehadalhroub.betawaytracker;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import static android.os.Process.THREAD_PRIORITY_BACKGROUND;

public class GpsService extends Service {

    private Looper mServiceLooper;
    private ServiceHandler mServiceHandler;
    private LocationManager locationManager;
    private int counter = 0;





    // Handler that receives messages from the thread
    private final class ServiceHandler extends Handler implements LocationListener {
        public ServiceHandler(Looper looper) {
            super(looper);
        }


        @SuppressLint("MissingPermission")
        @Override
        public void handleMessage(Message msg) {
            initializeLocationManager();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 3, this);


        }

        @Override
        public void onLocationChanged(Location loc) {
            System.out.println("Counter = " + ++counter);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }


    @Override
    public void onCreate() {
        // Start up the thread running the service. Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block. We also make it
        // background priority so CPU-intensive work doesn't disrupt our UI.

        HandlerThread thread = new HandlerThread("ServiceStartArguments",
                THREAD_PRIORITY_BACKGROUND);
        thread.start();

        // Get the HandlerThread's Looper and use it for our Handler
        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

        // For each start request, send a message to start a job and deliver the
        // start ID so we know which request we're stopping when we finish the job
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        mServiceHandler.sendMessage(msg);

        // If we get killed, after returning from here, restart
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
    }


    private void initializeLocationManager() {
        if (locationManager == null)
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }


}

1 个答案:

答案 0 :(得分:1)

greeble31答案:您的应用及其服务很可能被破坏,以释放资源。参见here。您可能想使用前台服务。