【问题标题】:android app crash on android 4.4 due to gps permission denial由于 gps 权限被拒绝,android 应用程序在 android 4.4 上崩溃
【发布时间】:2013-11-27 07:21:51
【问题描述】:

当我尝试在我的 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。

【问题讨论】:

  • 您是否尝试在语法上打开/关闭 GPS?
  • 是的,如果 GPS 关闭,我正在尝试手动启用它。并且工作正常到 4.3

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


【解决方案1】:

这从来都不是公共 API。

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

我猜,他们只是修复了整个安全问题。

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

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

【讨论】:

  • 您要求用户启用 GPS
  • @praveenSharma:我在答案中添加了 Intent。
  • 有没有像我一样使用intent启用gps Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
  • 不,没有。这是您正在使用的 API 中的一个错误。该错误已修复。
  • 您不明白这件事:错误 是,您可以通过广播更改设置。 fix 使这不再可能。它从来没有应该像你过去那样工作。
【解决方案2】:
   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;
}}

【讨论】:

  • 使用示例代码的答案很好,但是对于这么多的代码,最好添加一些关于它的作用、它来自哪里等的解释。
  • 如果您已经成功实现该代码,请避免在 4.4 中关闭自动 gps,那么我确信此代码将不适用于所有 4.4 设备,因此请使用此服务来使用 gps特征。完整答案链接stackoverflow.com/questions/22528984/…
  • 这个答案与问题有什么关系。我浪费时间分析,希望能找到有意义的东西。
猜你喜欢
  • 2023-03-26
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 2016-01-12
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
相关资源
最近更新 更多