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

时间:2014-10-23 10:48:55

标签: android gps

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

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

    }

}

1 个答案:

答案 0 :(得分:0)

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

double Latitude, Longitude;

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

在发送短信时,请将经度和纬度变量放在位置上。