更新时间:2023-08-29 21:41:46
获取当前位置绝不取决于 Android Studio 或您提到的其他内容。
所有您需要定义LocationListener
来处理位置更改:
Getting current location never depends on either Android Studio or other things as you mentioned.
All you need to define aLocationListener
to handle location changes:
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//your code here
}
};
然后获取 LocationManager
并询问位置更新:
Then get the LocationManager
and ask for location updates:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, mLocationListener);
}
确保您已在Manifest上添加了权限:
Make sure that you have added the permission on the Manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
您可以从这里阅读更多信息:
在Android中获取当前位置 [ ^ ]