Android 使用Wifi时查找当前位置有效,但使用GPS时无法查找当前位置
我开发了一个应用程序,可以使用GPS查找用户的位置 在这方面没有错误。它正确地使用Wifi查找位置,但在GPS模式下它不会返回任何值。我在清单中添加了所需的权限,GPS已打开 有人能告诉我如何使用Gps定位吗 我已在此处包含我的代码:Android 使用Wifi时查找当前位置有效,但使用GPS时无法查找当前位置,android,geolocation,Android,Geolocation,我开发了一个应用程序,可以使用GPS查找用户的位置 在这方面没有错误。它正确地使用Wifi查找位置,但在GPS模式下它不会返回任何值。我在清单中添加了所需的权限,GPS已打开 有人能告诉我如何使用Gps定位吗 我已在此处包含我的代码: package c.g.a.loation; public class UseGps extends Activity { @Override public void onCreate(Bundle savedInstanceState) {
package c.g.a.loation;
public class UseGps extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
Geocoder gp = new Geocoder(getBaseContext());
try {
ArrayList<Address> address = (ArrayList<Address>) gp
.getFromLocation(loc.getLatitude(), loc.getLongitude(),
1);
if (address != null && address.size() > 0) {
Toast.makeText(getBaseContext(), address.get(0).toString(),
Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
@Override
public void onProviderEnabled(String arg0) {}
}
public void onProviderEnabled(String provider) {
// Toast.makeText(
// getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
c.g.a.loation包;
公共类使用GPS扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LocationManager mlocManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
LocationListener mlocListener=新建MyLocationListener();
mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,
mlocListener);
}
/*类我的位置侦听器*/
公共类MyLocationListener实现LocationListener{
@凌驾
位置更改后的公共无效(位置loc){
地理编码器gp=新地理编码器(getBaseContext());
试一试{
ArrayList地址=(ArrayList)gp
.getFromLocation(loc.getLatitude(),loc.getLatitude(),
1);
if(address!=null&&address.size()>0){
Toast.makeText(getBaseContext(),address.get(0).toString(),
Toast.LENGTH_LONG).show();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){}
@凌驾
public void onStatusChanged(字符串arg0,int arg1,Bundle arg2){}
@凌驾
公共无效onProviderEnabled(字符串arg0){}
}
公共无效onProviderEnabled(字符串提供程序){
//Toast.makeText(
//getApplicationContext(),“启用Gps”,Toast.LENGTH_SHORT.show();
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){}
}
我有两个想法