activitycompat.requestpermissions不显示提示

d4so4syb  于 2021-07-03  发布在  Java
关注(0)|答案(4)|浏览(1650)

我正试图请求 ACCESS_FINE_LOCATION 获取用户当前位置的权限。
我的日志记录表明我的应用程序在查询时当前没有此权限 ContextCompat.checkSelfPermission() ,但是打电话的时候 ActivityCompat.requestPermissions() 不显示任何内容。
我的谷歌Map代码(实现) OnMapReadyCallback 以及 ActivityCompat.OnRequestPermissionsResultCallback() )在一个 FragmentActivity .
我设法得到了 requestPermissions() 功能在应用程序的其他活动中成功工作,它只是一个与谷歌Map。当它被放置在 onCreate() 方法 Activity ,或 onMapReady() (它需要去的地方)。

if(ContextCompat.checkSelfPermission(LocationActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "not granted");
        final String[] permissions = new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION};
    if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
            Log.d(TAG, "rationale");
            // Explain to the user why permission is required, then request again
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("We need permissions")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1);
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

    } else {
        Log.d(TAG, "request" + android.Manifest.permission.ACCESS_FINE_LOCATION);
        // If permission has not been denied before, request the permission
        ActivityCompat.requestPermissions(LocationActivity.this, permissions, 1);
    }
} else {
    Log.d(TAG, "granted");
}

有什么想法吗?这和我的活动课有关吗( FragmentActivity ),或者google map异步调用权限请求?

dojqjjoe

dojqjjoe1#

在将我的类完全剥离出来之后,它仍然不起作用,我意识到这个活动是使用tabhost示例化的。
当我停止使用tabhost时,将成功显示提示。我猜新的权限提示不支持tabhosts-这是一个bug吗?
与应用程序请求相同的问题没有出现
我最终创建了一个permissionsrequestactivity,它代表我的tabhost处理权限请求和响应,然后退出(通过intent extras bundle传入请求的权限信息)。
它将请求的响应作为广播传回,由我的tabhost接收。
有点黑客,但工作正常!

vawmfj5a

vawmfj5a2#

检查您是否已经在android的manifest文件中添加了请求的权限,就像在android m之前一样,只有这样您才能获得预期的行为。
将权限添加到清单中,以便您可以通过activitycompat.requestpermissions请求权限:

<uses-permission android:name="android.permission. ACCESS_FINE_LOCATION" />
amrnrhlw

amrnrhlw3#

我将分享对我有用的代码。在活动的protectedvoidoncreate(bundle savedinstancestate){}方法中,我希望看到提示,其中包含以下代码:

/* Check whether the app has the ACCESS_FINE_LOCATION permission and whether the app op that corresponds to
     * this permission is allowed. The return value is an int: The permission check result which is either
     * PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.
     * Source: https://developer.android.com/reference/android/support/v4/content/PermissionChecker.html
     * While testing, the return value is -1 when the "Your location" permission for the App is OFF, and 1 when it is ON.
     */
    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
    // The "Your location" permission for the App is OFF.
    if (permissionCheck == -1){
        /* This message will appear: "Allow [Name of my App] to access this device's location?"
         * "[Name of my Activity]._instance" is the activity.
         */
        ActivityCompat.requestPermissions([Name of my Activity]._instance, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ACCESS_FINE_LOCATION);
    }else{
        // The "Your location" permission for the App is ON.
        if (permissionCheck == 0){
        }
    }

在protectedvoid oncreate(bundle savedinstancestate){}方法之前,我创建了以下常量和方法:

public static final int REQUEST_CODE_ACCESS_FINE_LOCATION = 1; // For implementation of permission requests for Android 6.0 with API Level 23.

// Code from "Handle the permissions request response" at https://developer.android.com/training/permissions/requesting.html.
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ACCESS_FINE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // location-related task you need to do.                

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
ecbunoof

ecbunoof4#

我在一个使用tabhost的项目中也遇到过同样的问题。基于@robin解决方案,我使用eventbus库从子活动向tabactity发送消息。
事件总线:https://github.com/greenrobot/eventbus
创建事件对象:

public class MessageEvent {
    private String message;
    public MessageEvent(String message){
        this.message = message;
    }

    public String getMessage(){
        return this.message;
    }
}

在您的主要活动中:

private EventBus eventBus = EventBus.getDefault();
@Override
protected void onCreate(Bundle savedInstanceState) {
    eventBus.register(this);
}
@Override
protected void onDestroy() {
    eventBus.unregister(this);
    super.onDestroy();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    if (event.getMessage().equals("contacts")){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainPage.this,new String[]{android.Manifest.permission.WRITE_CONTACTS}, 100 );
        }
    }
};

为要请求的权限设置其他消息。在您的儿童活动中,您可以发布适当的信息:

EventBus.getDefault().post(new MessageEvent("contacts"));

注意onrequestpermissionsresult回调和请求代码;)!它只在主要活动中起作用。

相关问题