我是安卓新手。因此,如果我的问题很简单,请道歉并帮助我。
我正在开发一个 Android 应用程序,我尝试在其中获取用户位置仅使用 GPS 服务(因为我正在开发一个应用程序,即使使用没有互联网).
我的代码如下:
我的活动:
public class MyView extends Activity implements OnClickListener, Runnable
{
LocationManager itsLocationManager;
LocationListener itsLocationListener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
itsLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
itsLocationListener = new MyLocationListener(this, itsLocationManager);
getLocationAndSendMessage();
}
private void getLocationAndSendMessage()
{
try
{
itsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, itsLocationListener);
}
catch (Exception theException)
{
theException.printStackTrace();
ToastMsgUtil.showErrorMessage("Problem in retrieving your current location! Please try again.", this);
}
}
MyLocationListener.java:
public class MyLocationListener implements LocationListener
{
Context itsContext;
LocationManager itsLocationManager;
public MyLocationListener(Context theContext, LocationManager theLocationManager)
{
itsContext = theContext;
itsLocationManager = theLocationManager;
}
@Override
public void onLocationChanged(final Location theLocation)
{
//My Location processing code
itsLocationManager.removeUpdates(SafemateLocationListener.this);
}
}
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
正如我上面所说,我只需要通过 GPS 获取位置(因为用户的移动设备没有互联网)。
- 在调试时,我发现我是没有收到任何位置更新 in 位置改变时我使用时的方法LocationManager.GPS_PROVIDER.
- 但是,当我尝试使用LocationManager.NETWORK_PROVIDER,我收到位置更新。
谁能说我在上面的代码中做错了什么?我错过了什么吗?
请帮助。谢谢。