Utilisez ce code
boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
si vous avez du matériel GPS embarqué pour votre appareil, mais il n'est pas activé puis utilisez la morue suivante pour activer dynamiquement,
LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//Ask the user to enable GPS
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Manager");
builder.setMessage("Would you like to enable GPS?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Launch settings, allowing user to make a change
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//No location service, no Activity
finish();
}
});
builder.create().show();
}
Vous pouvez appeler LocationManager.getAllProviders()
et vérifiez si LocationManager.GPS_PROVIDER
est inclus dans la liste. Sinon vous utilisez simplement ce code LocationManager.isProviderEnabled(String provider)
méthode.
si return false même dans le cas si vous n `avez GPS dans votre appareil
J'ai un devide sans GPS, mais pm.hasSystemFeature (PackageManager.FEATURE_LOCATION_GPS); renvoie toujours la valeur True – skayred
voir la modification et essayez – Karthi
LocationManager.isProviderEnabled (Fournisseur de chaîne) renvoie false pour ma tablette, c'est-à-dire vrai. Mais si j'ai GPS désactivé sur mon ordinateur de poche avec le support GPS, ce sera faux aussi. J'ai besoin de vérifier la disponibilité du matériel, non activé-état désactivé – skayred