0

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?

user2027811
  • 4,691
  • 2
  • 14
  • 10

2 Answers2

1

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

Community
  • 1
  • 1
Tuan Vu
  • 6,397
  • 2
  • 14
  • 22
0

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.

Community
  • 1
  • 1
Nezam
  • 4,122
  • 3
  • 32
  • 49