Android 6.0.1定期获取gps位置失败

时间:2016-10-13 11:13:21

标签: android gps

我想创建用于记录GPS位置的后台服务。在执行方面,它不会给我任何定期数据,无论是在办公室,街道还是乡村。你能告诉我每隔n秒获取GPS位置的方法吗?我有时获得了Suppressed StrictMode策略违规的异常:StrictModeDiskReadViolation得到了。

我的代码设置如下:

    public class TaskpointActivity extends FragmentActivity implements  LocationListener,
      GoogleMap.InfoWindowAdapter   {

        private static final long POLLING_FREQ = 1000 * 2;


    private LocationManager mLocationManager  ;  
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {

...


    mLocationManager = (LocationManager)  this.getSystemService(Context.LOCATION_SERVICE);        

              mLocationManager.requestLocationUpdates(
                      LocationManager.GPS_PROVIDER, POLLING_FREQ,    1, this);

              mLocationManager.requestLocationUpdates(
                      LocationManager.NETWORK_PROVIDER, POLLING_FREQ,   1, this);

        } catch (Exception ex) { 
            SystemUtils.takeLog(this, ex.getMessage());
            ex.printStackTrace();
        }       
}

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onStatusChanged PROVIDER:" + provider + " STATUS:" + String.valueOf(status));

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onProviderEnabled PROVIDER:" + provider);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onProviderDisabled PROVIDER:" + provider);

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        if(mLastLocation !=null) {
               deviceSpeed = Math.sqrt(  Math.pow(location.getLongitude() - mLastLocation.getLongitude(), 2)
                               + Math.pow(location.getLatitude() - mLastLocation.getLatitude(), 2)
               ) / (location.getTime() - this.mLastLocation.getTime());
        }else{
              mLastLocation = location;
        }

          if (location.hasSpeed())
               deviceSpeed = location.getSpeed();

        SystemUtils.takeLog(this , "location latitude  : " + location.getLatitude() );
        SystemUtils.takeLog(this , "location longitude  : " + location.getLongitude()  );
        SystemUtils.takeLog(this , "location speed device  : " + location.getSpeed() );
        SystemUtils.takeLog(this , "location speed calculated  : " +  deviceSpeed  );

        LatLng startPosition = markerSingle!=null ? markerSingle.getPosition() :
                new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());

        if(executeFollowMe) {


            double latitu = startPosition.latitude;
            double longitu = startPosition.longitude  ;

            int fixed = 30;
            for(int i = 0 ; i < 30 ; i ++) {

                if(i < 10) { // east
                    longitu = longitu + (i+1) * 0.000003;
                }else if(i >=10 && i < 20) { // curve
                    int incre = i+1 ;
                    longitu = longitu +  incre  * 0.000003;
                    latitu = latitu + (fixed- incre) * 0.000003;
                }else if(i >=20 && i < 30) { // north
                    latitu = latitu + (i+1) * 0.000003;
                }
                LatLng currentPostion = new LatLng (latitu , longitu) ;
                prepareFollowMe(currentPostion);    
                //startPosition
            }

            SystemUtils.takeLog(TaskpointActivity.this,  markerSingle!=null ? "prepare Follow me mock" :  "prepare Follow me real device");
            //LatLng positionX

        }
    }

1 个答案:

答案 0 :(得分:0)

是的,我也遇到了这个问题。当我们使用

时会发生
  

targetSdkVersion 23

在我们的

  

的build.gradle

文件。

当您需要同时获取用户位置时,您需要获得权限运行时间,然后您可以使用

  

shouldShowRequestPermissionRationale()

此方法检查用户是否允许先前在我们的应用中允许,然后它将不再显示用户前面的权限框。它显示用户第一次给予许可或者他们以前是否给予丹尼。

有关详情,请查看:https://developer.android.com/training/permissions/requesting.html

还有一种解决方法就是设置你的

  

targetSdkVersion 22

insted of

  

targetSdkVersion 23