如何创建不断检查GPS状态的后台服务?

Posted

技术标签:

【中文标题】如何创建不断检查GPS状态的后台服务?【英文标题】:How to create background service which constantly check GPS status? 【发布时间】:2017-11-03 10:53:29 【问题描述】:

对于我的应用程序,GPS 是不断需要的。 如果 GPS 已关闭,那么我想向用户显示一条提示消息以打开 GPS。

我应该如何使用后台服务来执行这个任务?

【问题讨论】:

【参考方案1】:

服务,我从 *** 获取参考

public class MyService extends Service

    private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 1000;
    private static final float LOCATION_DISTANCE = 10f;

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

清单

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity android:label="@string/app_name" android:name=".LocationCheckerActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MyService" android:process=":my_service" />
</application>

【讨论】:

以上是关于如何创建不断检查GPS状态的后台服务?的主要内容,如果未能解决你的问题,请参考以下文章

在后台服务中使用融合位置获取GPS更新前(15分钟)?

Android服务在其仍处于活动状态时停止后台处理

Android 在设备处于睡眠状态时获取 GPS 坐标更新(屏幕已关闭)

如何不断地将 GPS 数据从 android 发送到服务器,直到应用程序关闭

尽管应用程序在 Android 上移至后台,但 GPS 仍保持打开状态

如何在应用程序处于后台时保持GPS接收器位置更新(Google Play服务位置API)