Search code examples
androidlocationcriteria

Android Secure the Location test


Hello everyone I am curious about how can I solve the location security problem in my code below:

 private void myLocaion(){
    Criteria criteria = new Criteria(); 
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    final String myBestProvider = locationManager.getBestProvider(criteria, false); 
    Toast.makeText(gtApplicationContext(), myBestProvider, Toast.LENTH_SHORT).show(); 

    myLocationListener = new LocationListener{ 

        public void onLocationChaned(Location location){
            Toas.makeText(getApplicationContext(),
                            location.getLatitude() + " " + 
                            location.getLongiude(), Toast.LENGTH_SHORT).show();
        }
        public void onStatusChanged(String provider, int status, Bundle extras){}
        public void onProviderEnabled(String provider){}
        public void onProviderDisabled(String provider){}
    };//LocationListener
    locationManager.requestLocationUpdates(MyBestProvider, 0, 0, myLocationListener)

}

It seems that development environment of Android Studio declaration on the security issue on this code:

locationManager.requestLocationUpdates(MyBestProvider, 0, 0, myLocationListener)

Android API: 23


Solution

  • Android API 23 Requires a security review before the operation code

    There is 3 ways to solve this problem:

    1) Try to give the development environment to repair the security code requirement

    2) To compile an older development environment Android Studio

    3) Change the version of the target

    Here is how to Change:

    File -> project Structure.. -> Flavors-> Targer Sdk Version Change SDK Version If everything is correct until now, You can advance in writing application And send the location information method onLocationChanged( ) To map function :

    Uri.parse("geo:" + myLocation.getLatitude() + "," myLocation.getLongitude())
    

    And then :

    public void showMap( Uri geoLocation ){
        Intent intent = new Intent (Intent.ACTION_VIEW); 
        Intent.setData(geoLocation); 
        if (intent.resolveActivity(getPackageManager()) != null){
            startActivity(intent); 
            locationManager.removeUpdates(mylocationListener;)
        }
    }