androidx.credentials.exceptions.GetCredentialCancellationException: 用户取消了活动。
创始人
2024-10-12 10:01:40
0

在处理 "androidx.credentials.exceptions.GetCredentialCancellationException: 用户取消了活动" 异常时,可以使用以下代码示例来解决问题:

import androidx.appcompat.app.AppCompatActivity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.credential.provider.Credential;
import androidx.credential.provider.CredentialClient;

import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSION_REQUEST_CODE = 1;
    private static final String[] REQUIRED_PERMISSIONS = {Manifest.permission.GET_ACCOUNTS};

    private CredentialClient mCredentialClient;

    private ActivityResultLauncher requestPermissionLauncher = registerForActivityResult(
            new ActivityResultContracts.RequestPermission(),
            isGranted -> {
                if (isGranted) {
                    // 用户授予了权限,可以继续获取凭据
                    getCredential();
                } else {
                    // 用户未授予权限,显示一个提示对话框或执行其他操作
                    showPermissionDeniedDialog();
                }
            });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCredentialClient = new CredentialClient(this);
        checkPermissions();
    }

    private void checkPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS)
                != PackageManager.PERMISSION_GRANTED) {
            // 如果没有获取到权限,则请求权限
            requestPermissionLauncher.launch(Manifest.permission.GET_ACCOUNTS);
        } else {
            // 已经获取到权限,可以继续获取凭据
            getCredential();
        }
    }

    private void getCredential() {
        mCredentialClient.request(new CredentialRequestOptions.Builder().build(),
                new CredentialClient.OnCredentialRetrievedCallback() {
                    @Override
                    public void onCredentialRetrieved(@Nullable Credential credential) {
                        // 在此处理获取到的凭据
                        if (credential != null) {
                            // 已获取到凭据
                            String username = credential.getId();
                            String password = credential.getPassword();
                            // 使用凭据执行其他操作
                        } else {
                            // 未获取到凭据
                        }
                    }

                    @Override
                    public void onCredentialRequestFailure(@NonNull Exception e) {
                        if (e instanceof GetCredentialCancellationException) {
                            // 用户取消了活动,可以执行相应操作,如显示一个提示或返回上一个界面
                            showCancellationDialog();
                        } else {
                            // 其他凭据请求失败的情况
                            Toast.makeText(MainActivity.this, "Credential request failed: " + e.getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void showCancellationDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("操作取消");
        builder.setMessage("用户取消了活动。");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 执行相应操作,如返回上一个界面
                finish();
            }
        });
        builder.show();
    }

    private void showPermissionDeniedDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("权限被拒绝");
        builder.setMessage("无法获取账户凭据,请授予应用程序获取账户的权限。");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 执行相应操作,如关闭应用程序
                finish();
            }
        });
        builder.show();
    }
}

这个代码示例中,首先检查是否已获取 GET_ACCOUNTS 权限,如果没有则请求权限。然后,使用 CredentialClient 请求凭据。如果用户取消了活动,则会抛出 GetCredentialCancellationException 异常,通过在 onCredentialRequestFailure 方法中判断异常类型,可以执行相应的操作,如显示一个对话框提示用户取消了活动。同时,还提供了一个方法 showPermissionDeniedDialog 显示权限被拒绝的对话框,提示用户授予应用程序获取账户的权限。

相关内容

热门资讯

透视有挂"wepok... 自定义wepoker养号规律系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器...
透视辅助"竞技联盟透... 透视辅助"竞技联盟透视"详细辅助2025新版教程(透视app下载辅助);致您一封信;亲爱竞技联盟透视...
透视私人局"佛手大菠... 1、透视私人局"佛手大菠萝13道挂哪里"详细辅助玩家教你(可以开辅助攻略)。2、佛手大菠萝13道挂哪...
透视安卓版"aapo... 透视安卓版"aapoker怎么开辅助器"详细辅助可靠技巧(可以提高运气神器);支持多人共享记分板与复...
透视黑科技"wepo... 透视黑科技"wepoker怎么挂飞机"详细辅助规律教程(游戏安装教程辅助)是一款可以让一直输的玩家,...
透视计算"智星德州插... 1、透视计算"智星德州插件最新版本更新内容详解"详细辅助解说技巧(开挂透明);详细教程。2、智星德州...
透视中牌率"wepo... 透视中牌率"wepoker的辅助器"详细辅助黑科技教程(有辅助透视);最新版2024是一款经典耐玩的...
透视黑科技"wepo... 透视黑科技"wepoker有辅助插件吗"详细辅助曝光教程(透视有用透明)准备好在wepoker有辅助...
透视线上"hhpok... 透视线上"hhpoker辅助挂是真的吗"详细辅助2025版教程(有插件神器)1、不需要AI权限,帮助...
透视规律"uupok... 透视规律"uupoker透视"详细辅助微扑克教程(有挂软件);最新版2024是一款经典耐玩的益智游戏...