Android 如何在后台使用gps服务运行15分钟?

Android 如何在后台使用gps服务运行15分钟?,android,gps,android-service,Android,Gps,Android Service,你好,我是android开发的新手,我需要的是在15分钟内获取用户位置并发送到服务器,我知道我可以使用该服务。实施后,当我从最近的内存中清除应用程序时,我停止更新我阅读并尝试了所有解决方案即使清除了最近的应用程序,我如何实现这一点需要确保我的服务正在运行并获取用户位置让我发布我的服务代码: public class GPSService extends Service { private static final String TAG = "BOOMBOOMTESTGPS"; p

你好,我是android开发的新手,我需要的是在15分钟内获取用户位置并发送到服务器,我知道我可以使用该服务。实施后,当我从最近的内存中清除应用程序时,我停止更新我阅读并尝试了所有解决方案即使清除了最近的应用程序,我如何实现这一点需要确保我的服务正在运行并获取用户位置让我发布我的服务代码:

public class GPSService extends Service
{
    private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 10000;
    private static final float LOCATION_DISTANCE = 0;

    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);
        }
    }

    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 onTaskRemoved(Intent rootIntent){
                super.onTaskRemoved(rootIntent);
        Intent restartService = new Intent(getApplicationContext(),
                this.getClass());
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);

    }
    @Override
    public void onDestroy()
    {
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        Intent intent = new Intent("restartApps");
        sendBroadcast(intent);
        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);
        }
    }
}
我的舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="precisioninfomatics.backgroundgps">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:name=".gspapp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".GPS"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyLocationService"
            android:exported="true"
            android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
            </intent-filter>
        </service>
        <service
            android:name=".GPSService"
            android:enabled="true"
            android:stopWithTask="false"
            android:exported="false" />
        <receiver
            android:name=".RestartService"
            android:enabled="true" >
            <intent-filter>
                <action android:name="restartApps" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

我的问题是:

1。即使在清除我最近的应用程序后,仍需要获取用户位置 2.如果用户在设置中强制停止,如何重新启动我的服务


提前感谢

使用start\u Stick service启动服务,这样,如果它因为某些优先级而被终止,而不是重新创建,并且如果您想在后台继续运行此服务并唤醒该服务,请锁定该服务,并使用Alarm Manager检查它是否正在运行。

您可以使用AlarmManager来计划您的服务。比如:

AlarmManager alarm =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent checkLocationIntent = new Intent(context, CheckLocationService.class);

    PendingIntent pendingIntent = PendingIntent.getService(context, 0, checkLocationIntent, 0);

    alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ALARM_INTERVAL_MILLISECONDS,
            ALARM_INTERVAL_MILLISECONDS, pendingIntent);
这将在每个报警间隔毫秒时间运行CheckLocationService

AlarmManager alarm =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent checkLocationIntent = new Intent(context, CheckLocationService.class);

    PendingIntent pendingIntent = PendingIntent.getService(context, 0, checkLocationIntent, 0);

    alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, ALARM_INTERVAL_MILLISECONDS,
            ALARM_INTERVAL_MILLISECONDS, pendingIntent);