Maps API V2位置更新需要很长时间

时间:2014-04-18 22:17:45

标签: android google-maps gps location

我有一个奇怪的问题,我似乎无法弄明白。如果我在手机上打开地图应用程序,我几乎可以立即得到修复,但另一方面我的应用程序有时需要很长时间才能得到修复,或者在某些情况下甚至拒绝完全执行此操作!我正在使用FragmentActivity btw。这是我的代码:

的onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    setUpMapIfNeeded();

    //This keeps the screen On while app is running
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


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

            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();       
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setSpeedRequired(false);       

            // Getting the name of the best provider
            String bestProvider = lm.getBestProvider(criteria, true);

    // Getting Current Location with GPS
    //loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
    //lm.requestLocationUpdates(lm.GPS_PROVIDER, 1000, 1, this);

    loc = lm.getLastKnownLocation(bestProvider);
    lm.requestLocationUpdates(bestProvider, 1000, 1, this);

    Log.d(TAG, "Location " + loc);

    broadcastReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent){
            Object tmp = intent.getParcelableExtra(ActivityRecognitionService.RECOGNITION_RESULT);
            addUpdate((ActivityRecognitionResult)tmp);
        }
    };

}

正如你所看到的,我尝试将GPS指定为提供商但由于我使用的手机只有GPS和Wifi(基本上是平板电脑),所以它并没有真正改变。忽略那些用于活动识别的broadcastReceiver事件,如Still,Walking,In_Vehicle等。

setupMapIfNeeded和setupMap

private void setUpMapIfNeeded() {
    // TODO Auto-generated method stub

    // Do a null check to confirm that we have not already instantiated the map.
    if (map == null) 
    {
        // Try to obtain the map from the SupportMapFragment.
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.

        if (map != null) 
        {
            setUpMap();
        }

        //This is how you register the LocationSource
        map.setLocationSource(this);
    }

}

/**
 * This is where we can add markers or lines, add listeners or move the camera.
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() 
{
    map.setMyLocationEnabled(true);

    map.setOnMapLongClickListener(this);
}

这一切都很直接。

onLocationChangedListener

Override
public void activate(OnLocationChangedListener locationChangedListener) {
    // TODO Auto-generated method stub
    locListener = locationChangedListener;

}


@Override
public void deactivate() {
    // TODO Auto-generated method stub
    locListener = null;
}
@Override
public void onLocationChanged(Location loc) {

    //Push location updates to the registered listener
    //This ensures my-location layer retrieves the new/received location
    if (locListener != null) {
        locListener.onLocationChanged(loc);
        Utils.showInfoMessage(this, "Got a location");

        Log.d(TAG, "onLocationChanged");

        if(loc.hasSpeed()){
            float speed = loc.getSpeed();
            Log.d(TAG, "Speed = " + speed);
        };

        //Animate camera to center of phone location
        map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(loc.getLatitude(), loc.getLongitude())));


        //              Toast.makeText(getApplicationContext(), "Location Changed",
        //                      Toast.LENGTH_SHORT).show();

    }

}

所以是的,我不知道为什么会这样做但我觉得这与我在Logcat的All Messages过滤器中发现这些重复出错有关。其中一人说:

Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@42a36178 that was originally bound here

另一个说:

java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL

有人可以帮帮我吗?它变得非常令人沮丧,因为有一天应用程序似乎在下一个它没有工作,它正在把我送到墙上!这也是我的Uni论文!

1 个答案:

答案 0 :(得分:0)

我已将我的应用程序更改为新项目,新API密钥和新软件包名称。出于某种原因,这解决了我的问题!我的应用程序启动后几乎立即得到修复!很奇怪。