2015-12-02 59 views
0

我想獲取用戶的位置,但每當我運行此代碼時,它都會返回「無法獲取位置」。我不知道爲什麼這不起作用。我在Android Studio的默認模擬器上運行它。我做錯了什麼?爲什麼我的代碼不停地返回「無法獲取位置?」

import android.Manifest; 
import android.app.Activity; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

public class MainActivity extends Activity implements LocationListener { 

LocationManager locationManager; 
String provider; 

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

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    provider = locationManager.getBestProvider(new Criteria(), false); 

    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for Activity#requestPermissions for more details. 
     return; 
    } 
    Location location = locationManager.getLastKnownLocation(provider); 

    if (location != null) { 

     Log.i("Location Info", "Location achieved!"); 

    } else { 

     Log.i("Location Info", "Unable to get location"); 

    } 

} 


@Override 
protected void onResume() { 
    super.onResume(); 

    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for Activity#requestPermissions for more details. 
     return; 
    } 
    locationManager.requestLocationUpdates(provider, 400, 1, this); 

} 

@Override 
protected void onPause() { 
    super.onPause(); 

    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for Activity#requestPermissions for more details. 
     return; 
    } 
    locationManager.removeUpdates(this); 

} 

@Override 
public void onLocationChanged(Location location) { 

    Double lat = location.getLatitude(); 
    Double lng = location.getLongitude(); 

    Log.i("Location info: Lat", lat.toString()); 
    Log.i("Location info: Lng", lng.toString()); 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

public void getLocation(View view) { 

    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for Activity#requestPermissions for more details. 
     return; 
    } 
    Location location = locationManager.getLastKnownLocation(provider); 

    onLocationChanged(location); 


} 

}

+0

位置/ GPS已關閉? – JJF

+0

不,我只是確保位置已打開並啓用 – felix

回答

0

如果你使用Eclipse試試這個,去Window - >Open Perspective - >DDMS - >Emulator Control,然後鍵入一個在Location Controls和命中Send

+0

即時通訊使用Android Studio – felix

+0

我剛剛檢查它也適用於Android Studio,只需轉到Android設備管理器。應該是您的工具欄上的圖標。然後執行'Emulator Control',然後在'Location Controls'中輸入一個,然後點擊'發送'。 – user5581557

+0

只嘗試使用粗略位置,因爲這不會使用GPS。如果您在建築物內,通常不會獲取GPS信號。 – user5581557

相關問題