如何在屏幕休眠时让Android应用在后台运行?

时间:2017-04-27 06:31:35

标签: android google-maps background-process android-maps android-wake-lock

我正在开发一个跟踪应用程序,通过每隔3秒获取当前位置来保持用户的跟踪。我可以在屏幕打开时获取lat long值。但是当屏幕睡觉时我无法获取数据。

CODE:

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    if (mCurrLocationMarker != null)
    {
        mCurrLocationMarker.remove();
    }
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    latLngcurrent = new LatLng(location.getLatitude(), location.getLongitude());

    Toast.makeText(context,String.valueOf(latitude)+" "+String.valueOf(longitude), Toast.LENGTH_LONG).show();

    Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f",latitude,longitude));


    Log.d("onLocationChanged", "Exit");
    Toast.makeText(this, "exiting LocationChanged ", Toast.LENGTH_SHORT).show();
    }

上面给出的onLocationChanged方法每3秒获取一次lat lon值。即使屏幕休眠,我该怎么做才能使这个方法运行。我读过关于Wakelock不知道如何实现它,任何帮助将不胜感激。提前谢谢。

注意:当多任务处理完成后,该应用程序可以正常运行,

3 个答案:

答案 0 :(得分:2)

使用作业计划程序每隔3秒获取一次位置,它在屏幕睡眠https://developer.android.com/reference/android/app/job/JobScheduler.html

时也能正常工作

答案 1 :(得分:1)

即使屏幕睡眠或您的应用未打开,您也需要使用Service来获取位置更新。

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MyLocationService extends Service
{
    private static final String TAG = "MyLocationService";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 3000;
    private static final float LOCATION_DISTANCE = 10f;

    LocationListener[] mLocationListeners = new LocationListener[] {
            new LocationListener(LocationManager.GPS_PROVIDER),
            new LocationListener(LocationManager.NETWORK_PROVIDER)
    };

    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onCreate()
    {
        Log.e(TAG, "onCreate");
        initializeLocationManager();
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[1]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "network provider does not exist, " + ex.getMessage());
        }
        try {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                    mLocationListeners[0]);
        } catch (java.lang.SecurityException ex) {
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex) {
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        }
    }

    @Override
    public void onDestroy()
    {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) {
            for (int i = 0; i < mLocationListeners.length; i++) {
                try {
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                } catch (Exception ex) {
                    Log.i(TAG, "fail to remove location listners, ignore", ex);
                }
            }
        }
    }

    private void initializeLocationManager() {
        Log.e(TAG, "initializeLocationManager");
        if (mLocationManager == null) {
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }

    // create LocationListener class to get location updates
    private class LocationListener implements android.location.LocationListener
    {
        Location mLastLocation;

        public LocationListener(String provider)
        {
            Log.e(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        }

        @Override
        public void onLocationChanged(Location location)
        {
            Log.e(TAG, "onLocationChanged: " + location);
            mLastLocation.set(location);
        }

        @Override
        public void onProviderDisabled(String provider)
        {
            Log.e(TAG, "onProviderDisabled: " + provider);
        }

        @Override
        public void onProviderEnabled(String provider)
        {
            Log.e(TAG, "onProviderEnabled: " + provider);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            Log.e(TAG, "onStatusChanged: " + provider);
        }
    }
}

MyLocationService添加到您的AndroidManifest.xml

<service android:name=".MyLocationService" android:process=":mylocation_service" />

请勿忘记在AndroidManifest.xml文件中添加以下两项权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

从活动中启动服务:

您可以通过将Intent(指定要启动的服务)传递给service,从活动或其他应用程序组件启动startService()。 Android系统调用服务的onStartCommand()方法并将其传递给Intent。

Intent intent = new Intent(this, MyLocationService.class);
startService(intent);

<强>更新

即使关闭设备屏幕,您仍需要按住partial wake lock才能运行服务。

如果您保持部分唤醒锁定,CPU将继续运行,无论显示超时或屏幕状态如何,甚至在用户按下电源按钮后也是如此。

获取唤醒锁定:

PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();

要发布它:

wakeLock.release();

答案 2 :(得分:0)

尝试使用ServiceJobScheduler并实施LocationListener

@Override
public void onLocationChanged(Location location) {
   mLastLocation = location;
   if (mCurrLocationMarker != null)
   {
      mCurrLocationMarker.remove();
   }
   latitude = location.getLatitude();
   longitude = location.getLongitude();
   latLngcurrent = new LatLng(location.getLatitude(),location.getLongitude());

   Toast.makeText(context,String.valueOf(latitude)+" "+String.valueOf(longitude), Toast.LENGTH_LONG).show();

   Log.d("onLocationChanged", String.format("latitude:%.3f longitude:%.3f",latitude,longitude));

   Log.d("onLocationChanged", "Exit");
   Toast.makeText(this, "exiting LocationChanged ", Toast.LENGTH_SHORT).show();
}