【问题标题】:Android Location service didn't work in backgroundAndroid 定位服务无法在后台运行
【发布时间】:2016-04-27 09:34:10
【问题描述】:

我正在开发一个位置应用程序,当用户按下Button 时,它应该开始根据经度和纬度跟踪一些统计数据。所有这些东西都很好用,但是当涉及到锁定屏幕或将应用程序置于后台时,该服务不再工作了!

我已经阅读了很多关于后台服务和广播接收器的信息,但我不知道如何在服务中实现 Google API 位置侦听器以及在MainActivity 中的何处实现此类。谁能用一个简短的代码示例告诉我如何实现这样的服务或解释这一点的链接?

【问题讨论】:

    标签: android service background location


    【解决方案1】:

    使用保险丝定位服务,每次都保存更新的位置

    public class LocationNotifyService extends Service implements
            LocationListener,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
    
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        public static Location mCurrentLocation;
    
        .............................
    
        @Override
        public void onCreate() {
    
            //show error dialog if GoolglePlayServices not available
            if (isGooglePlayServicesAvailable()) {
                mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(BACKGROUND_INTERVAL);  
            mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            //mLocationRequest.setSmallestDisplacement(10.0f);  /* min dist for location change, here it is 10 meter */
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .build();
    
                mGoogleApiClient.connect();
            }
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
             return super.onStartCommand(intent, flags, startId);
        }
    
        //Check Google play is available or not
        private boolean isGooglePlayServicesAvailable() {
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
            return ConnectionResult.SUCCESS == status;
        }
    
    
        @Override
        public void onConnected(Bundle bundle) {
            startLocationUpdates();
        }
    
        protected void startLocationUpdates() {
            try {
                 PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                        mGoogleApiClient, mLocationRequest, this);
            } catch (IllegalStateException e) {}
        }
    
        @Override
        public void onLocationChanged(Location location) {
    
            //Save your location
        }
        ............................
    }
    

    你可以得到当前位置onLocationChanged()

    更多详情请查看http://javapapers.com/android/android-location-fused-provider/或关注官方指南https://developer.android.com/training/location/index.html

    【讨论】:

    • 谢谢,这正是我正在寻找的!如何在 MainActivity 中调用这个 Service 来显示数据?
    • 如何在 MainActivity 中调用接收者以及如何使用 Service 在 MainActivity 中提供的数据?
    • 在 SharedPreference 中保存位置 lat long 并在 onLocationChanged() 方法中继续编辑它。您可以通过调用 startService(new Intent(YourActivity.this, LocationNotifyService.class)); 从 Activity 启动服务
    • 我应该在 MainAcitivity 的哪个函数中调用包含最新位置的 SharedPreferences?是否有一个函数可以在服务(位置)中的某些内容发生变化时触发?
    • @madrid11 如何在特定时间触发此操作
    【解决方案2】:

    这是执行您想要的操作的应用程序:

    https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/CosyDVR.java

    Main Activity 在 BackgroundVideoRecorder 服务上启动。

    Intent intent = new Intent(/*CosyDVR.this*/getApplicationContext(), BackgroundVideoRecorder.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startService(intent);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    

    查看 mConnection onServiceConnected,onServiceDisconnected。

    这是 BackgroundVideoRecorder 类:

    https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/BackgroundVideoRecorder.java

    它实现了 LocationListener。 onLocationChanged、onProviderDisabled、onProviderEnabled、onStatusChanged 也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 2022-07-27
      相关资源
      最近更新 更多