android:如何使用gps获取更新的位置

android:如何使用gps获取更新的位置,android,gps,Android,Gps,我正在制作一个应用程序,在固定时间后通过短信发送用户的当前位置,但它总是发送相同的坐标。我读了很多链接,但我不知道哪里是错误的,请告诉我我的代码有什么问题,你可以编辑你喜欢的代码 public class AlarmReceiver extends BroadcastReceiver implements LocationListener { long time = 600* 1000; long distance = 10; boolean isGPSEnab

我正在制作一个应用程序,在固定时间后通过短信发送用户的当前位置,但它总是发送相同的坐标。我读了很多链接,但我不知道哪里是错误的,请告诉我我的代码有什么问题,你可以编辑你喜欢的代码

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;
    String phoneNo = "+923362243969";

    @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) {
                        location.getLatitude();
                        location.getLongitude();

                        String Text =  " From GPS: Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " 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 = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " 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

    }

    @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

    }

}

这是您需要获取位置的地方。将经度和纬度变量作为公共成员,并在下面的事件中更新它们

double Latitude, Longitude;

    @Override
        public void onLocationChanged(Location location) {
            Longitude = location.getLongitude();
            Latitude = location.getLatitude();
        }

发送短信时,将经度和纬度变量替换为位置。

更新onLocationChanged事件中的坐标以获取最新位置。请给我看几行即时消息谢谢。那么如何在if语句中调用这个方法呢?如果位置管理器!=null{location=locationManager.GetLastKnownLocationManager.GPS\提供程序;如果位置!=null{location.getLatitude;location.getLongitudei guess在此事件中不是必需的。因为位置更改意味着没有测试有效的坐标。如果您已经测试了当前代码,则一切正常:我的意思是说此语句将保持原样?如果locationManager!=null{location=locationManager.getLastKnownLocationLocationManager.GPS\u Provider;如果位置!=null{location.getLatitude;location.getLongitudethankss。请回答更多问题,此行也将保持不变?字符串文本=来自网络:纬度=+location.getLatitude+经度=+location.getLatitude;