我试图在Android中获取当前位置。至少,我从这篇文章中拿了这个例子。
How do I get the current GPS location programmatically in Android?
虽然这篇文章已有五年历史,但是commonsware告诉我要问一个新问题。 这是我的代码片段。
@Override
public void onCreate(Bundle arg0) {
super.onCreate(arg0);
Log.i(TAG, "onCreate");
setHasOptionsMenu(true);
mActivity = (CustomerTabs) this.getActivity();
mActivity.getActionBar().hide();
if (checkGooglePlayServicesAvailable()){
enableTabs(false);
showDialog(true, null);
requestPosition();
}
boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getActivity());
Log.i(TAG,
"checkGooglePlayServicesAvailable, connectionStatusCode="
+ connectionStatusCode);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
//showDialog(false,null);
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
void showGooglePlayServicesAvailabilityErrorDialog(
final int connectionStatusCode) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
try {
final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
connectionStatusCode, getActivity(),
101);
if (dialog == null) {
Log.e(TAG,
"couldn't get GooglePlayServicesUtil.getErrorDialog");
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
void requestPosition(){
Log.i(TAG,"requestPosition");
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
60000, 1000,
onLocationChange);
}catch (Exception e){
e.printStackTrace();
showDialog(false, null);
enableTabs(true);
displayMessage(getString(R.string.position_fail));
}
}
LocationListener onLocationChange=new LocationListener() {
@Override
public void onLocationChanged(android.location.Location location) {
Log.i(TAG, "onLocationChange " + location.getLatitude() + "/" + location.getLongitude());
TemporaryPersister.getInstance().setCurrentPosition(new LatLng(location.getLatitude(),location.getLongitude()));
requestLocations();
}
清单上的权限:
<permission
android:name="versatec.organightandroid.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="versatec.organightandroid.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES "/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
修改
此解决方案正在开发4.1(S3 mini),有无WiFi,但无法在WiFi中使用另一台设备(S3:没有SIM卡 - 问题?)。 出现对话框,调用requestPosition() - 注意发生......
我从测试人员那里获得了反馈,它在4.2 - S4,网络可用时无效(没有任何反应)。
日志:
I/Google Maps Android API﹕ Google Play services package version: 6599036
I/Map﹕ onCreate
I/Map﹕ checkGooglePlayServicesAvailable, connectionStatusCode=0
I/Map﹕ showDialog true
I/Map﹕ requestPosition
I/Map﹕ onCreateView
I/Map﹕ onActivityCreated
答案 0 :(得分:0)
这是因为:locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,....) 您只使用了网络提供商...
如果您想确保获得位置,请注册其他提供商。