user2027811
user2027811

Reputation: 4931

LocationManager.getLastKnownLocation returns null when it runs just after system booted

my appwidget gets location to get forecast. as title, cant get location at just after system rebooted.

    LocationManager lm = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setBearingRequired(false); 
    criteria.setSpeedRequired(false);   
    criteria.setAltitudeRequired(false);    
    String provider = lm.getBestProvider(criteria, true);
    Location loc = lm.getLastKnownLocation(provider);

    double lat = loc.getLatitude(); //null
    double lon = loc.getLongitude();/null

why these are null at just after system rebooted?

Upvotes: 0

Views: 733

Answers (2)

Nezam
Nezam

Reputation: 4132

It is because on Boot up both GPS and Wifi/Data Connection is OFF.So,location is null. You need to change a bit of code which can prompt the user to open GPS Settings or Wifi Settings.

Upvotes: 0

Tuan Vu
Tuan Vu

Reputation: 6447

Using Broadcastreceiver on reboot

// Do something onReceive()

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

//Do something after reboot
}

See this link:

Android BroadcastReceiver on startup - keep running when Activity is in Background

Upvotes: 1

Related Questions