在mainavtivity中增加定位权限申请
This commit is contained in:
@@ -39,103 +39,7 @@ public class MainIndependentActivity extends MainActivity {
|
||||
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() {
|
||||
|
||||
@@ -61,7 +61,6 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
|
||||
Log.d(TAG, "onCreate");
|
||||
Log.d("liyz", "MainLauncherActivity onCreate ------->");
|
||||
mServiceApis.getV2XListenerManager().registerIntentListener(MogoReceiver.ACTION_V2X_FRONT_WARNING, this);
|
||||
checkPermission();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -261,104 +260,4 @@ 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.mogo.module.main;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
@@ -42,6 +47,8 @@ import com.zhidao.autopilot.support.api.AutopilotServiceManage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.mogo.module.main.MainPresenter.MOGO_PERMISSION_REQUEST_CODE;
|
||||
|
||||
/**
|
||||
* @author congtaowang
|
||||
* @since 2019-12-23
|
||||
@@ -76,6 +83,8 @@ public class MainActivity extends MvpActivity<MainView, MainPresenter> implement
|
||||
|
||||
protected ConstraintLayout clSpecialEffect;
|
||||
|
||||
private boolean isFirst = false;
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
if (!DebugConfig.isMapBased()) {
|
||||
@@ -155,6 +164,8 @@ public class MainActivity extends MvpActivity<MainView, MainPresenter> implement
|
||||
NetworkUtils.listenNetStrength(this);
|
||||
DisplayEffectsHelper.getInstance().init(clSpecialEffect);
|
||||
mServiceApis.getStatusManagerApi().registerStatusChangedListener(TAG, StatusDescriptor.VR_MODE, this);
|
||||
|
||||
mPresenter.checkPermission(this);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
@@ -426,4 +437,55 @@ public class MainActivity extends MvpActivity<MainView, MainPresenter> implement
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == MOGO_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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package com.mogo.module.main;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
|
||||
import com.mogo.commons.mvp.Presenter;
|
||||
@@ -37,6 +43,8 @@ public class MainPresenter extends Presenter< MainView > {
|
||||
}
|
||||
};
|
||||
|
||||
public static final int MOGO_PERMISSION_REQUEST_CODE = 10000;
|
||||
|
||||
public MainPresenter( MainView view ) {
|
||||
super( view );
|
||||
SchemeIntent.getInstance().init( getContext(), mView.getApis() );
|
||||
@@ -47,6 +55,46 @@ public class MainPresenter extends Presenter< MainView > {
|
||||
super.onCreate( owner );
|
||||
}
|
||||
|
||||
public void checkPermission(Activity activity) {
|
||||
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(
|
||||
activity,
|
||||
new String[]{
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}, MOGO_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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 延时操作
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user