我正在尝试很多天,但不知道我的代码有什么问题,它给了我相同的位置,从来没有从gps获得更新的位置我知道这个问题有很多anwsers我已经尝试了很多解决方案但是我仍然得到同样的东西。请我们请求人们查看我的代码,看看哪里有错误,我们将非常感谢任何帮助。
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
}
}
答案 0 :(得分:0)
当locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
没有位置更新时,行LocationManager
会提供最后知道的位置。我猜你总是从这里得到位置。如果您正在使用模拟器,可以通过查找位置来尝试不同的位置;如果您有真实设备,可以通过物理移动来尝试不同的位置: - /
答案 1 :(得分:0)
当你从GPS获得位置时,可能需要一些时间,因为GPS模块需要找到卫星。因此,当GPS模块找到卫星时,您需要等待几分钟或几分钟。如果WI-FI位置提供商可用,您可以更快地获得位置。
如果您在建筑物内,可能很难获得GPS位置。这取决于设备和设备GPS模块。
我在这里看不到逻辑:
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time,distance, this);
为什么在注册GPS提供商监听器之前需要检查位置是否为null?