代码之家  ›  专栏  ›  技术社区  ›  DontPanic

权限:调用需要用户拒绝的权限

  •  1
  • DontPanic  · 技术社区  · 7 年前

    在android studio下编译后,我得到了本文标题中指定的警告。我尝试使用以下代码获取GPS更新:

        try {
            LocMgr = (LocationManager)getSystemService( LOCATION_SERVICE );
            LocMgr.requestLocationUpdates( K.GPS_PROVIDER, 0, 0, this );
            //LocMgr.addNmeaListener( this );   // NMEA
        }
        catch(Exception e) {
            UI.warningBox( this, "GPS appears to be either absent or disabled" );
        }
    

    奇怪的是,我的应用程序仍然 工作得很好 没有任何运行时权限请求。我 但是,请在清单中请求权限。

        uses-permission  android:name="android.permission.ACCESS_FINE_LOCATION"
        uses-permission  android:name="android.permission.ACCESS_COARSE_LOCATION"
    

    我并不不高兴,但我期待着失败或者至少是安全异常。

    为什么Android Studio如此(出乎意料地)宽容?

    顺便说一下,我模块的Gradle脚本包括

        defaultConfig {
        applicationId "MY_APP_ID"
        minSdkVersion 11
        targetSdkVersion 26
        }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   guipivoto SadeghAlavizadeh    7 年前

    您的应用程序没有在运行时请求权限,因为您没有将代码添加到。此外,最近的Android上增加了运行时权限(我想是从Android M开始的)。

    因此,在以前的Android版本中,您的应用程序将正常工作,因为您只需要向清单添加权限。

    在新版本上,必须在运行时另外请求权限。没有许可并不总是导致崩溃。他们只能在日志等上发出警告。

    对于任何情况,要消除该警告,必须在运行时请求权限。下面,您可以找到一个小例子:

    public class MainActivity extends AppCompatActivity implements LocationListener {
    
        private final static int LOCATION_PERMISSION_CODE = 1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            ....
    
            try {
                LocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    
                if (ActivityCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
                      && ActivityCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // Missing both permissions. User didn't accept them yet. Request
                    ActivityCompat.requestPermissions(this, new String[] {permission.ACCESS_FINE_LOCATION, permission.ACCESS_COARSE_LOCATION}, LOCATION_PERMISSION_CODE);
                } else {
                    // Have permissions. Don't need to request. Just request location
                    LocMgr.requestLocationUpdates( K.GPS_PROVIDER, 0, 0, this );
                }
            } catch(Exception e) {
                UI.warningBox( this, "GPS appears to be either absent or disabled" );
            }
    
            ....
        }
    
        @Override
        public void onRequestPermissionsResult(final int requestCode,
                @NonNull final String[] permissions,
                @NonNull final int[] grantResults) {
            if(requestCode == LOCATION_PERMISSION_CODE) {
                if(grantResults.length > 1
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED /* ACCESS_FINE_LOCATION */
                        && grantResults[1] == PackageManager.PERMISSION_GRANTED /* ACCESS_COARSE_LOCATION */) {
                    // User accepted both permissions. request location updates
                    LocMgr.requestLocationUpdates( K.GPS_PROVIDER, 0, 0, this );
                }
            }
        }
    }
    
        2
  •  1
  •   kkarakk    7 年前

    你用什么版本的Android来运行你的应用程序?在棉花糖(API级别23)之前,您可以访问locationservice等许多服务,而无需实际请求用户的许可。

    你甚至可以在没有请求许可的情况下使用WiFi和蓝牙。那时候它是权限的西部。从那时起,这就发生了变化,因为您的最高级别API可能会请求权限(您没有处理这些权限),这就是您收到警告的原因。

    另外,请注意,如果您有权访问Google服务,您应该使用FusedLocationProvider,而locationService在不创建服务来连续轮询它的情况下不会给出有效的结果。

    推荐文章