Code to check if GPS is active

2

Good morning, I'm developing an application that checks for vulnerabilities in Android and would like to know if anyone knows of an open source application or know the code that verifies that the GPS is active or check all applications on the device and let them know if they are using their GPS.

Thank you

    
asked by anonymous 30.01.2015 / 13:06

2 answers

7

To know if GPS is connected use this code:

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

I think there is no way to know which applications are currently using GPS. You can, however, know which apps are allowed to use it:

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

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

To get the list of permissions use, for each of the appInstaladas items:

//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.com

    
30.01.2015 / 15:44
1

Do 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 customer can choose whether or not to activate the GPS. But read what the Ramaral spoke.

    
03.06.2016 / 19:54