请求清单文件中声明的所有权限

Posted

技术标签:

【中文标题】请求清单文件中声明的所有权限【英文标题】:request all the permission declared in the manifest file 【发布时间】:2021-09-09 11:51:47 【问题描述】:

我正在使用 xamarin 构建 android 应用程序问题我对 c# 不太熟悉...所以我试图让我的应用程序请求我已经在清单文件中一次声明的所有权限 我试过这个,但它只请求相机权限

if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera) ==(int)Permission.Granted) 

// We have permission, go ahead and use the camera.
 
else 

// Camera permission is not granted. If necessary display rationale & request.
 
    

如何让这段代码请求所有声明的权限?

【问题讨论】:

【参考方案1】:
ActivityCompat.requestPermissions(this, new String[] Manifest.permission.Camera, 1);

您可以在数组中请求更多您想要的权限。另外,请注意 1 是请求代码。

有关更多信息,请参阅documentation。

【讨论】:

你的意思是我必须写所有的权限?在数组中? 请求代码怎么样... int 1 是一样的还是随着每个权限的变化而变化 无论权限如何,您都可以将其设置为您想要的任何值(常用1)。 你能给我一个3的例子或数组中的权限吗【参考方案2】:

对此,可以参考官方文档:Permissions In Xamarin.Android。

还有一个关于此的示例,您可以在这里查看:https://github.com/xamarin/monodroid-samples/tree/master/android-m/RuntimePermissions。

如果要定义两个以上的权限,可以参考下面的代码sn-ps(这里我们要在我们的设备中读写cotacts):

    /**
    * Id to identify a contacts permission request.
    */
    static readonly int REQUEST_CONTACTS = 1;

    /**
    * Permissions required to read and write contacts. Used by the ContactsFragment.
    */
    static string[] PERMISSIONS_CONTACT = 
        Manifest.Permission.ReadContacts,
        Manifest.Permission.WriteContacts
    ;

    public void ShowContacts (View v)
    
        Log.Info (TAG, "Show contacts button pressed. Checking permissions.");

        // Verify that all required contact permissions have been granted.
        if (ActivityCompat.CheckSelfPermission (this, Manifest.Permission.ReadContacts) != (int)Permission.Granted
            || ActivityCompat.CheckSelfPermission (this, Manifest.Permission.WriteContacts) != (int)Permission.Granted) 
            // Contacts permissions have not been granted.
            Log.Info (TAG, "Contact permissions has NOT been granted. Requesting permissions.");
            RequestContactsPermissions ();
         else 
            // Contact permissions have been granted. Show the contacts fragment.
            Log.Info (TAG, "Contact permissions have already been granted. Displaying contact details.");
            ShowContactDetails ();
        
    

方法:RequestContactsPermissions

    void RequestContactsPermissions ()
    
        if (ActivityCompat.ShouldShowRequestPermissionRationale (this, Manifest.Permission.ReadContacts)
            || ActivityCompat.ShouldShowRequestPermissionRationale (this, Manifest.Permission.WriteContacts)) 

            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example, if the request has been denied previously.
            Log.Info (TAG, "Displaying contacts permission rationale to provide additional context.");

            // Display a SnackBar with an explanation and a button to trigger the request.
            Snackbar.Make (layout, Resource.String.permission_contacts_rationale,
                Snackbar.LengthIndefinite).SetAction (Resource.String.ok, new Action<View> (delegate(View obj) 
                    ActivityCompat.RequestPermissions (this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
                )).Show ();
         else 
            // Contact permissions have not been granted yet. Request them directly.
            ActivityCompat.RequestPermissions (this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
        
    

方法OnRequestPermissionsResult

    public override void OnRequestPermissionsResult (int requestCode, string[] permissions, Permission[] grantResults)
    
         if (requestCode == REQUEST_CONTACTS) 
            Log.Info (TAG, "Received response for contact permissions request.");

            // We have requested multiple permissions for contacts, so all of them need to be
            // checked.
            if (PermissionUtil.VerifyPermissions (grantResults)) 
                // All required permissions have been granted, display contacts fragment.
                Snackbar.Make (layout, Resource.String.permission_available_contacts, Snackbar.LengthShort).Show ();
             else 
                Log.Info (TAG, "Contacts permissions were NOT granted.");
                Snackbar.Make (layout, Resource.String.permissions_not_granted, Snackbar.LengthShort).Show ();
            

         else 
            base.OnRequestPermissionsResult (requestCode, permissions, grantResults);
        
    

更多详细信息,您可以查看示例:https://github.com/xamarin/monodroid-samples/tree/master/android-m/RuntimePermissions。

【讨论】:

以上是关于请求清单文件中声明的所有权限的主要内容,如果未能解决你的问题,请参考以下文章

Android清单文件具体解释 ---- 应用程序权限声明

webRequest

清单中的请求旧版外部存储权限 - Google Play 控制台 5 月 5 日消息

如何在android中获取用户权限而不将其声明为清单

再次请求位置权限 onRequestPermissionsResult

清单文件中缺少 Cordova 相机权限