web-dev-qa-db-fra.com

Comment demander à l'utilisateur d'activer le GPS lors du lancement de l'application?

private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(!provider.contains("gps")){ //if gps is disabled
    final Intent poke = new Intent();
    poke.setClassName("com.Android.settings", "com.Android.settings.widget.SettingsAppWidgetProvider"); 
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
}
}

voir le logcat

04/20 17:35:26: Launching app
$ adb Push F:\AndroidProject\Picker\app\build\outputs\apk\app-debug.apk /data/local/tmp/picker.novasyslabs.com.picker
$ adb Shell pm install -r "/data/local/tmp/picker.novasyslabs.com.picker"
    pkg: /data/local/tmp/picker.novasyslabs.com.picker
Success


$ adb Shell am start -n "picker.novasyslabs.com.picker/picker.novasyslabs.com.picker.SlashScreen" -a Android.intent.action.MAIN -c Android.intent.category.LAUNCHER
Connected to process 22502 on device sony-d2302-ZH80065A89
W/ResourceType: Found multiple library tables, ignoring...
W/ResourceType: Found multiple library tables, ignoring...
W/ResourceType: Found multiple library tables, ignoring...
W/art: Before Android 4.1, method Android.graphics.PorterDuffColorFilter Android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(Android.graphics.PorterDuffColorFilter, Android.content.res.ColorStateList, Android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in Android.graphics.drawable.Drawable
I/art: Background partial concurrent mark sweep GC freed 151(19KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 13MB/22MB, paused 5.156ms total 20.919ms
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: picker.novasyslabs.com.picker, PID: 22502
                  Java.lang.RuntimeException: Unable to start activity ComponentInfo{picker.novasyslabs.com.picker/picker.novasyslabs.com.picker.SlashScreen}: Java.lang.ClassCastException: picker.novasyslabs.com.picker.SlashScreen cannot be cast to com.google.Android.gms.common.api.GoogleApiClient$ConnectionCallbacks
                      at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2379)
                      at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2442)
                      at Android.app.ActivityThread.access$800(ActivityThread.Java:156)
                      at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1351)
                      at Android.os.Handler.dispatchMessage(Handler.Java:102)
                      at Android.os.Looper.loop(Looper.Java:211)
                      at Android.app.ActivityThread.main(ActivityThread.Java:5371)
                      at Java.lang.reflect.Method.invoke(Native Method)
                      at Java.lang.reflect.Method.invoke(Method.Java:372)
                      at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:945)
                      at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:740)
                   Caused by: Java.lang.ClassCastException: picker.novasyslabs.com.picker.SlashScreen cannot be cast to com.google.Android.gms.common.api.GoogleApiClient$ConnectionCallbacks
                      at picker.novasyslabs.com.picker.SlashScreen.onCreate(SlashScreen.Java:65)
                      at Android.app.Activity.performCreate(Activity.Java:5990)
                      at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1106)
                      at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2332)
                      at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2442) 
                      at Android.app.ActivityThread.access$800(ActivityThread.Java:156) 
                      at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1351) 
                      at Android.os.Handler.dispatchMessage(Handler.Java:102) 
                      at Android.os.Looper.loop(Looper.Java:211) 
                      at Android.app.ActivityThread.main(ActivityThread.Java:5371) 
                      at Java.lang.reflect.Method.invoke(Native Method) 
                      at Java.lang.reflect.Method.invoke(Method.Java:372) 
                      at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:945) 
                      at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:740) 
Application terminated.

J'ai essayé beaucoup d'autres options comme le gestionnaire de paquets, le gestionnaire d'emplacement, etc. mais je ne suis pas capable de faire ce que je veux . Je veux que lors du lancement de l'application, l'utilisateur sera invité à activer le GPS . Je ne veux pas que l'utilisateur aille dans les paramètres.

Et que je voulais pour le niveau API> = 19.

S'il vous plaît aidez-moi à résoudre mon problème ......... Merci

7
Mishu
 GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    googleApiClient.connect();

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(5 * 1000);
    locationRequest.setFastestInterval(2 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);

    //**************************
    builder.setAlwaysShow(true); //this is the key ingredient
    //**************************

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(@NonNull LocationSettingsResult result) {
            final Status status = result.getStatus();
//                final LocationSettingsStates state = result.getLocationSettingsStates();

            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:


                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                context, 1000);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    break;
            }
        }
    });
10
Dheerubhai Bansal

utilisez SettingsApi si vous ne voulez pas forcer l’utilisateur à aller dans la page des paramètres pour activer le GPS. voici le lien pour savoir comment utiliser SettingsApi SettingsApi

2
Hitesh Gehlot