2014-04-18 35 views
0

我有一个奇怪的问题,我似乎无法弄清楚。如果我在手机上打开地图应用程序,我几乎可以马上得到修复程序,但另一方面,我的应用程序有时需要很长时间才能修复,有时候甚至拒绝彻底修复!我使用FragmentActivity btw。这里是我的代码:地图API V2位置更新需要很长的时间

的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(基本上是一款平板电脑)。忽略广播接收器的东西那里仍然是为动作识别像,散步,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中的所有邮件过滤器这一反复出现的错误。其中一人说:

Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]36178 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论文!

+0

经过一番测试,我发现“map.setLocationSource(this);”是这个问题的罪魁祸首。我不明白为什么艰难或如何解决它。 –

回答

0

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

相关问题