由于gps权限拒绝,Android应用程序在Android 4.4上崩溃

时间:2013-11-27 07:21:51

标签: android android-intent android-4.4-kitkat

当我尝试在我的Android 4.4 (Kitkat)上启用GPS时,我的Android应用程序崩溃了。它一直运行良好,直到Android 4.3。 我正在使用以下代码打开GPS

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.sendBroadcast(intent);

在我的Log Cat中给出安全例外。

我的LogCat详细信息如下: -

11-27 12:47:37.410: E/AndroidRuntime(3818): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=3818, uid=10084
11-27 12:47:37.410: E/AndroidRuntime(3818):  at android.os.Parcel.readException(Parcel.java:1461)
11-27 12:47:37.410: E/AndroidRuntime(3818):  at android.os.Parcel.readException(Parcel.java:1415)
11-27 12:47:37.410: E/AndroidRuntime(3818):  at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2373)
11-27 12:47:37.410: E/AndroidRuntime(3818):  at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
11-27 12:47:37.410: E/AndroidRuntime(3818):  at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
11-27 12:47:37.410: E/AndroidRuntime(3818):  at com.sus.SUSV7_1.Activity.Splash_ScreenActivity.turnGPSOn(Splash_ScreenActivity.java:66)
11-27 12:47:37.410: E/AndroidRuntime(3818):  at com.sus.SUSV7_1.Activity.Splash_ScreenActivity.onCreate(Splash_ScreenActivity.java:26)

当我评论代码时,它的工作正常。是否有任何特定参数可在Android 4.4上手动启用GPS。

2 个答案:

答案 0 :(得分:11)

这绝不是公共API。

AOSP错误跟踪器上甚至存在关于此主题的问题: https://code.google.com/p/android/issues/detail?id=35924

我猜,他们只是解决了整个安全问题。

您可能希望引导用户更改GPS设置:

startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

答案 1 :(得分:-7)

   public class GPSService extends Service implements LocationListener {

// saving the context for later use
private final Context mContext;

// if GPS is enabled
boolean isGPSEnabled = false;
// if Network is enabled
boolean isNetworkEnabled = false;
// if Location co-ordinates are available using GPS or Network
public boolean isLocationAvailable = false;

// Location and co-ordinates coordinates
Location mLocation;
double mLatitude;
double mLongitude;

// Minimum time fluctuation for next update (in milliseconds)
private static final long TIME = 30000;
// Minimum distance fluctuation for next update (in meters)
private static final long DISTANCE = 20;

// Declaring a Location Manager
protected LocationManager mLocationManager;

public GPSService(Context context) {
    this.mContext = context;
    mLocationManager = (LocationManager) mContext
            .getSystemService(LOCATION_SERVICE);

}

/**
 * Returs the Location
 * 
 * @return Location or null if no location is found
 */
public Location getLocation() {
    try {

        // Getting GPS status
        isGPSEnabled = mLocationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // If GPS enabled, get latitude/longitude using GPS Services
        if (isGPSEnabled) {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, TIME, DISTANCE, this);
            if (mLocationManager != null) {
                mLocation = mLocationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (mLocation != null) {
                    mLatitude = mLocation.getLatitude();
                    mLongitude = mLocation.getLongitude();
                    isLocationAvailable = true; // setting a flag that
                                                // location is available
                    return mLocation;
                }
            }
        }

        // If we are reaching this part, it means GPS was not able to fetch
        // any location
        // Getting network status
        isNetworkEnabled = mLocationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (isNetworkEnabled) {
            mLocationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, TIME, DISTANCE, this);
            if (mLocationManager != null) {
                mLocation = mLocationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (mLocation != null) {
                    mLatitude = mLocation.getLatitude();
                    mLongitude = mLocation.getLongitude();
                    isLocationAvailable = true; // setting a flag that
                                                // location is available
                    return mLocation;
                }
            }
        }
        // If reaching here means, we were not able to get location neither
        // from GPS not Network,
        if (!isGPSEnabled) {
            // so asking user to open GPS
            askUserToOpenGPS();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    // if reaching here means, location was not available, so setting the
    // flag as false
    isLocationAvailable = false;
    return null;
}

/**
 * Gives you complete address of the location
 * 
 * @return complete address in String
 */
public String getLocationAddress() {

    if (isLocationAvailable) {

        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        // Get the current location from the input parameter list
        // Create a list to contain the result address
        List<Address> addresses = null;
        try {
            /*
             * Return 1 address.
             */
            addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
        } catch (IOException e1) {
            e1.printStackTrace();
            return ("IO Exception trying to get address:" + e1);
        } catch (IllegalArgumentException e2) {
            // Error message to post in the log
            String errorString = "Illegal arguments "
                    + Double.toString(mLatitude) + " , "
                    + Double.toString(mLongitude)
                    + " passed to address service";
            e2.printStackTrace();
            return errorString;
        }
        // If the reverse geocode returned an address
        if (addresses != null && addresses.size() > 0) {
            // Get the first address
            Address address = addresses.get(0);
            /*
             * Format the first line of address (if available), city, and
             * country name.
             */
            String addressText = String.format(
                    "%s, %s, %s",
                    // If there's a street address, add it
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "",
                    // Locality is usually a city
                    address.getLocality(),
                    // The country of the address
                    address.getCountryName());
            // Return the text
            return addressText;
        } else {
            return "No address found by the service: Note to the developers, If no address is found by google itself, there is nothing you can do about it.";
        }
    } else {
        return "Location Not available";
    }

}



/**
 * get latitude
 * 
 * @return latitude in double
 */
public double getLatitude() {
    if (mLocation != null) {
        mLatitude = mLocation.getLatitude();
    }
    return mLatitude;
}

/**
 * get longitude
 * 
 * @return longitude in double
 */
public double getLongitude() {
    if (mLocation != null) {
        mLongitude = mLocation.getLongitude();
    }
    return mLongitude;
}

/**
 * close GPS to save battery
 */
public void closeGPS() {
    if (mLocationManager != null) {
        mLocationManager.removeUpdates(GPSService.this);
    }
}

/**
 * show settings to open GPS
 */
public void askUserToOpenGPS() {
    AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    mAlertDialog.setTitle("Location not available, Open GPS?")
    .setMessage("Activate GPS to use use location services?")
    .setPositiveButton("Open Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
            }
        })
        .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                }
            }).show();
}

/** 
 * Updating the location when location changes
 */
@Override
public void onLocationChanged(Location location) {
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}}