问题描述
在MIUI13系统的手机中,会存在非小米商店下载的应用次启动时,会请求用户授权网络访问权限,若用户未授权并跳过,应用将无法访问网络,下次启动不再弹出授权弹框,用户不知道具体该如何操作,通过客服会告知用户需要手动进如系统设置进行相关授权。此过程会造成部分用户不知道如何操作。
解决方案
在应用主Activity的onCreate()方法中,增加网络是否开启的测试,如果未通过网络测试则弹出授权窗口,引导用户跳到对应系统进行权限设置。
private void checkNetworkConnectivity(final Activity activity) { new OkHttpClient().newCall(new Request.Builder() .head() .url("https://www.baidu.com") // 指定任意可访问域名 .build()) .enqueue(new okhttp3.Callback() { @Override public void onFailure(Call call, IOException e) { if (e instanceof ConnectException) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { showNetworkFailedDialog(activity); } }); } } @Override public void onResponse(Call call, Response response) { } }); } private void showNetworkFailedDialog(final Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { new AlertDialog.Builder(activity) .setMessage("获取⽹络权限失败") .setPositiveButton("去设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { try { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.fromParts("package", activity.getPackageName(), null)); startActivity(intent); } catch (Exception e) { } finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).show(); } }
checkNetworkConnectivity要在onCreate中调用,次解决方案在一定程度上可以解决用户遇到的问题。