I trying to get the location using network/gps.
I define add the permission on the manifest ( ACCESS_FINE_LOCATION ) but i getting that the permission is -1 ( PERMISSION_DENIED )
I calling a instance of the class 'GetLocation' on the main activity. And call the 'getCurrentLocation(this)' from this main activity.
The code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TraceActionLocation t = new TraceActionLocation();
Location l = t.getCurrentLocation(this);
}
public class GetLocation
{
public Location getCurrentLocation(Context context)
{
Location location = null;
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Boolean isGpsEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Boolean isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGpsEnable && !isNetworkEnable)
{
// TODO !!! => no gps and no network !!!
}
else if(context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_DENIED)
{
// 1. get the location from network provider
if(isNetworkEnable)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
// 2. get the more equate location from the gps
if(isGpsEnable)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
return location;
}
}