2014-04-18 36 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密鑰和新包名稱。出於某種原因,這已經解決了我的問題!我幾乎在我的應用程序啓動後立即得到修復!很奇怪。

相關問題