Android:关闭wifi后从网络获取位置

时间:2014-11-13 05:41:14

标签: java android gps

我必须通过短信发送用户的位置。我想首先从gps获取位置,如果gps不可用,那么它应该从网络发送位置。但我的应用程序同时发送两条消息,一条来自gps,另一条来自网络,虽然我的设备关闭了wifi网络但我不知道它从哪里获取位置。 我希望它首先只使用gps,如果它没有找到gps,那么从网络发送消息。这是我的代码。

public class AlarmReceiver extends BroadcastReceiver implements LocationListener  {

long time = 0 * 1000; 
long distance = 0; 
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
Location location;
String device_id;
double latitude ;
double longitude;
protected LocationManager locationManager;
// flag for GPS status
boolean canGetLocation = false;
String phoneNo = "+923300000009";

@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)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 && !isNetworkEnabled) {
        // no network provider is enabled
    } else {
        this.canGetLocation = true;
        // First check from GPS 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 = " + 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(); 

                    }
                }
            }
        }
    }
       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) {
                    latitude = location.getLatitude();
                    longitude = 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) {
    e.printStackTrace();
}

}  
public boolean canGetLocation() {
    return this.canGetLocation;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
        // this.location = location;

}

@Override
public void onProviderDisabled(String provider) {}

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}

0 个答案:

没有答案