提高minSdkVersion
解决定位权限导致的ANR
This commit is contained in:
@@ -12,7 +12,7 @@ ext {
|
||||
commonIndependentAmapApiValue : "1c3fbc5f5e183619ffb1e7bc01e6751f",
|
||||
compileSdkVersion : 28,
|
||||
buildToolsVersion : "29.0.2",
|
||||
minSdkVersion : 22,
|
||||
minSdkVersion : 23,
|
||||
targetSdkVersion : 23,
|
||||
]
|
||||
dependencies = [
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.zhidao.mogo.module.main.independent">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
|
||||
|
||||
<application>
|
||||
<activity
|
||||
android:name=".MainIndependentActivity"
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
package com.zhidao.mogo.module.main.independent;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.mogo.module.common.map.CustomNaviInterrupter;
|
||||
import com.mogo.module.main.MainActivity;
|
||||
@@ -20,12 +31,111 @@ import com.mogo.utils.UiThreadHandler;
|
||||
*/
|
||||
public class MainIndependentActivity extends MainActivity {
|
||||
|
||||
|
||||
private boolean isFirst = false;
|
||||
|
||||
@Override
|
||||
protected void onCreate( @Nullable Bundle savedInstanceState ) {
|
||||
super.onCreate( savedInstanceState );
|
||||
// 独立app需要在onCreate里面增加处理scheme的情况
|
||||
mPresenter.handleSchemeIntent( getIntent(), false );
|
||||
|
||||
checkPermission();
|
||||
}
|
||||
private void checkPermission() {
|
||||
Log.e("llh---","checkPermission");
|
||||
boolean isAllGranted = checkPermissionAllGranted(
|
||||
new String[]{
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}
|
||||
);
|
||||
if (isAllGranted) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求权限
|
||||
*/
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
new String[]{
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否拥有指定的所有权限
|
||||
*/
|
||||
private boolean checkPermissionAllGranted(String[] permissions) {
|
||||
for (String permission : permissions) {
|
||||
if (ContextCompat.checkSelfPermission(getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
// 只要有一个权限没有被授予, 则直接返回 false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请权限结果返回处理
|
||||
*/
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == 1) {
|
||||
boolean isAllGranted = true;
|
||||
// 判断是否所有的权限都已经授予了
|
||||
for (int grant : grantResults) {
|
||||
Log.d("liyz", "grant =" + grant);
|
||||
if (grant != PackageManager.PERMISSION_GRANTED) {
|
||||
isAllGranted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Log.d("liyz", "onRequestPermissionsResult isAllGranted = " + isAllGranted);
|
||||
if (isAllGranted) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
// 弹出对话框告诉用户需要权限的原因, 并引导用户去应用权限管理中手动打开权限按钮
|
||||
if (!isFirst) {
|
||||
openAppDetails();
|
||||
isFirst = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开APP的详情设置
|
||||
*/
|
||||
private void openAppDetails() {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setMessage("请在 “应用信息 -> 权限” 中授予权限");
|
||||
builder.setPositiveButton("手动授权", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
intent.setData(Uri.parse("package:" + getPackageName()));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton("取消", null);
|
||||
builder.show();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void initViews() {
|
||||
|
||||
@@ -59,7 +59,9 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
|
||||
super.onCreate(savedInstanceState);
|
||||
DebugConfig.setNeedRequestUserInfo(true);
|
||||
Log.d(TAG, "onCreate");
|
||||
Log.d("liyz", "MainLauncherActivity onCreate ------->");
|
||||
mServiceApis.getV2XListenerManager().registerIntentListener(MogoReceiver.ACTION_V2X_FRONT_WARNING, this);
|
||||
checkPermission();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -259,4 +261,104 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean isFirst = false;
|
||||
private static final int MY_PERMISSION_REQUEST_CODE = 10000;
|
||||
|
||||
/**
|
||||
* 检查是否有相应的权限
|
||||
*/
|
||||
private void checkPermission() {
|
||||
boolean isAllGranted = checkPermissionAllGranted(
|
||||
new String[]{
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}
|
||||
);
|
||||
Log.d("liyz", "MainLauncherActivity checkPermission -------> isAllGranted = " + isAllGranted);
|
||||
if (isAllGranted) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求权限
|
||||
*/
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
new String[]{
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}, MY_PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否拥有指定的所有权限
|
||||
*/
|
||||
private boolean checkPermissionAllGranted(String[] permissions) {
|
||||
for (String permission : permissions) {
|
||||
if (ContextCompat.checkSelfPermission(getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
// 只要有一个权限没有被授予, 则直接返回 false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请权限结果返回处理
|
||||
*/
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == MY_PERMISSION_REQUEST_CODE) {
|
||||
boolean isAllGranted = true;
|
||||
// 判断是否所有的权限都已经授予了
|
||||
for (int grant : grantResults) {
|
||||
Log.d("liyz", "grant =" + grant);
|
||||
if (grant != PackageManager.PERMISSION_GRANTED) {
|
||||
isAllGranted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Log.d("liyz", "onRequestPermissionsResult isAllGranted = " + isAllGranted);
|
||||
if (isAllGranted) {
|
||||
isFirst = false;
|
||||
} else {
|
||||
// 弹出对话框告诉用户需要权限的原因, 并引导用户去应用权限管理中手动打开权限按钮
|
||||
if (!isFirst) {
|
||||
openAppDetails();
|
||||
isFirst = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开APP的详情设置
|
||||
*/
|
||||
private void openAppDetails() {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setMessage("请在 “应用信息 -> 权限” 中授予权限");
|
||||
builder.setPositiveButton("手动授权", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
intent.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
intent.setData(Uri.parse("package:" + getPackageName()));
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
builder.setNegativeButton("取消", null);
|
||||
builder.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user