Android 如何从gps获取位置信息
我尝试了很多天,但不知道我的代码中有什么错误,它给了我相同的位置,从未从gps获得更新的位置。我知道这个问题有很多答案,我尝试了很多解决方案,但我仍然得到了同样的答案。请我请求你们查看我的代码,看看哪里是错误的任何帮助将不胜感激Android 如何从gps获取位置信息,android,gps,location,Android,Gps,Location,我尝试了很多天,但不知道我的代码中有什么错误,它给了我相同的位置,从未从gps获得更新的位置。我知道这个问题有很多答案,我尝试了很多解决方案,但我仍然得到了同样的答案。请我请求你们查看我的代码,看看哪里是错误的任何帮助将不胜感激 public class AlarmReceiver extends BroadcastReceiver implements LocationListener { long time = 600* 1000; long distance = 10; bool
public class AlarmReceiver extends BroadcastReceiver implements LocationListener {
long time = 600* 1000;
long distance = 10;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
Location location;
String device_id;
double latitude;
double longitude;
String phoneNo = "+980070000";
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("alarm receiver....");
Intent service = new Intent(context, MyService.class);
context.startService(service);
//Start App On device restart
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent App = new Intent(context, MainActivity.class);
App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(App);
}
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
device_id = tm.getDeviceId(); // returns IMEI number
try {
LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time,distance, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
String Text = " From GPS: Latitude = " + latitude +" Longitude = " + longitude + " Device Id: " + device_id;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, Text, null, null);
Log.i("Send SMS", "");
this.abortBroadcast();
}
}
}
else {
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time,distance, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
location.getLatitude();
location.getLongitude();
String Text = " From Network: Latitude = " + latitude +" Longitude = " + longitude + " Device Id: " + device_id;
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, Text, null, null);
Log.i("Send SMS", "");
this.abortBroadcast();
}
}
}
}
} catch (Exception e) {
Toast.makeText(context, "no connection", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
System.out.println("location changed....");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}行
locationManager.getLastKnownLocation(locationManager.GPS\U提供程序)当LocationManager
中没有位置更新时,code>提供最后知道的位置。我猜你总是能从这件事中得到位置。如果您正在使用emulator,则可以通过更改位置来尝试不同的位置;如果您有真实的设备,则可以通过实际移动来尝试不同的位置:-/当您从GPS获取位置时,可能需要一些时间,因为GPS模块需要查找卫星。所以,当GPS模块找到卫星时,您需要等待几分钟。如果WI-FI位置提供商可用,您可以更快地获取位置信息
如果你在一座建筑物内,很难获得GPS定位。这取决于设备和设备GPS模块
我看不出有什么逻辑:
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time,distance, this);
为什么在注册GPS提供程序侦听器之前需要检查位置是否为空?我在不同的位置尝试过,它总是给出相同的位置,所以这一行没有用?