Code to check if GPS is active

Asked

Viewed 4,640 times

2

Good morning, I am developing an application that check vulnerabilities on Android and would like to know if anyone knows an application with open source or knows the code that checks if the GPS is active or check all the applications of the device and warn if they are using your GPS.

thank you

2 answers

7

To know if the GPS is on use this code:

LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
bool isOn = manager.isProviderEnabled( LocationManager.GPS_PROVIDER);  

I do not think there is any way of knowing which applications are currently using GPS.
It is possible, however, to know which applications are allowed to use it:

PackageManager p = context.getPackageManager(); 
List<PackageInfo> appInstaladas = p.getInstalledPackages(PackageManager.GET_PERMISSIONS);

This code fills the list appInstaladas with information about installed applications, including their permissions.

For the list of permissions use, for each of the appInstaladas:

//Retorna permissões declaradas em <uses-permission> 
String[] requestedPermissions = appInstaladas.get(index).requestedPermissions();

//Retorna permissões declaradas em <permission>
PermissionInfo[] permissions = appInstaldas.get(index).permissions(); 

Sources:
PackageInfo.requestedPermissions
Packageinfo.Permissions
SO AS.

  • Thank you very much Ramaral I will test abs...

1

Do it like this:

LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);

// Verifica se o GPS está ativo
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

// Caso não esteja ativo abre um novo diálogo com as configurações para
// realizar se ativamento
if (!enabled) {
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}

You can create a method with a dialog so that the client can choose whether or not to activate the GPS. But take a look at what the Ramaral spoke.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.