简体   繁体   中英

Location Service not working fine in some Android devices

I am using the following code to show user a dialog box if the device's GPS location isdisabled.

final AlertDialog.Builder builder =
        new AlertDialog.Builder(activity);
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        final String message = "Enable GPS"
        + " service to find current location. And change the location mode to high accuracy for the app to perform accurately. Click OK to go to"
        + " location services settings to let you do so.";

        builder.setMessage(message)
        .setPositiveButton("OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface d, int id) {
                activity.startActivity(new Intent(action));
                d.dismiss();
            }
        })
        .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface d, int id) {
                d.cancel();
            }
        });
        builder.create().show();

So far I have tested this code on two devices MotoG (v 4.4.4) and Samsung duos (v 4.1.2).

I have my GPS ON in both of my devices. In MotoG is gives me the correct result back which is true but in the samsung device it returns false.

But the samsung device however is returning me the address (I am connecting to location service to get current address). I am getting the address in onConnected method of the location client. If its returning the address then the GPS should be enabled right?

Why is this does any one know about this issue ?

Try this

public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // Setting Icon to Dialog
    //alertDialog.setIcon(R.drawable.delete);

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM