Я новичок в Android и пытаюсь привести один пример для определения местоположения GPS. Я занимаюсь серфингом через Интернет и имею следующий код Программа нажмет sh, как только я нажму кнопку, чтобы получить GPS. Я обнаружил, что что-то не так, когда я вызываю locationManager.requestLocationUpdates (commandStr, 1000, 0, locationListener); Я прослеживаю его в requestLocationUpdates и обнаруживаю, что он выходит из строя в классе Looper
public final class Looper {
Looper(boolean quitAllowed) {
throw new RuntimeException("Stub!");
}
public static void prepare() {
throw new RuntimeException("Stub!");
}
public static void prepareMainLooper() {
throw new RuntimeException("Stub!");
}
public static Looper getMainLooper() {
throw new RuntimeException("Stub!");
}
public static void loop() {
throw new RuntimeException("Stub!");
}
Ниже приведен полный код, если кто-нибудь знает, какая часть программы неправильна
public class MainActivity extends AppCompatActivity {
private TextView tvLocation;
private Button btGetLocation;
private LocationManager locationManager;
private String commandStr;
private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11;
public LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
tvLocation.setText("經度" + location.getLongitude() + "\n緯度" + location.getLatitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
commandStr = LocationManager.GPS_PROVIDER;
// LocationManager.GPS_PROVIDER //使用GPS定位
// LocationManager.NETWORK_PROVIDER //使用網路定位
tvLocation = (TextView) findViewById(R.id.textView);
btGetLocation = (Button) findViewById(R.id.button);
btGetLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// LocationManager可以用來獲取當前的位置,追蹤設備的移動路線
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// 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 ActivityCompat#requestPermissions for more details.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COARSE_LOCATION);
return;
}
locationManager.requestLocationUpdates(commandStr, 1000, 0, locationListener);
// 事件的條件設定為時間間隔1秒,距離改變0米,設定監聽位置變化
Location location = locationManager.getLastKnownLocation(commandStr);
if (location != null)
tvLocation.setText("經度"+location.getLongitude()+"\n緯度"+location.getLatitude());
else
tvLocation.setText("定位中");
// location.getLongitude() //取得經度
// location.getLatitude() //取得緯度
// location.getAltitude() //取得海拔高度
}
});
}
}