Get current location during app launch

前端 未结 4 1578
再見小時候
再見小時候 2020-12-16 04:29

Good Day! I am working on an android app which monitors user location. I am using LocationManager to get users location, using the following method

public vo         


        
4条回答
  •  臣服心动
    2020-12-16 04:41

    Use this technique :

    LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    
    boolean network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
    Location location;
    
    if(network_enabled){
    
       location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
    if(location!=null){
       longitude = location.getLongitude();
       latitude = location.getLatitude();
        }                
    }
    

    In this case you even no need to on GPS only your mobile network will do.

    Don't forget to give the following permission in Manifest:

    
    
    
    

提交回复
热议问题