android:没有getLastKnownLocation()获取当前位置

时间:2014-10-20 08:15:43

标签: android gps

我的应用程序通过短信发送硬编码号码的gps坐标,但它总是发送相同的坐标,从不给出新的位置。我不知道我在哪里弄错了,请帮我解决问题。

public class AlarmReceiver extends BroadcastReceiver implements LocationListener{

long time = 900 * 1000; 
long distance = 10; 
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
Location location;

@SuppressLint("NewApi") 

@Override
public void onReceive(final Context context, Intent intent) {

    System.out.println("alarm receiver....");
    Intent service = new Intent(context, MyService.class);
    context.startService(service);

    //Start App On Boot Start Up
    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);
    }

    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();


 TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                        String device_id = tm.getDeviceId();  // returns IMEI number  

                        String phoneNo = "+923409090000";
                        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();

   TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                                    String device_id = tm.getDeviceId();  // returns IMEI number  
                                    String phoneNo = "+9234090900000";

                                    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

}

}

0 个答案:

没有答案