我正在尝试我的应用程序,但当我在我的手机中安装apk时,首先启动它显示权限快速崩溃,当我重新启动它显示权限,我检查确定,它的确定。
请问如何预防?因为有些用户不会再次重启应用程序!谢谢
这是我的权限:(仅在记录音频,访问文件时出现问题)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
有关项目的信息:
minSdkVersion 15
targetSdkVersion 26
compileSdkVersion 26
非常感谢
错误日志中的错误:
11-21 17:46:17.298 24237-24647/com.geniusly.myapp E/Message_Register: DONE
11-21 17:46:17.476 24237-24237/com.geniusly.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.geniusly.myapp, PID: 24237
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=200, result=0, data=null} to activity {com.geniusly.myapp/tin.subtil.HomeActivityNew}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at android.app.ActivityThread.deliverResults(ActivityThread.java:4481)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4524)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1691)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at tin.subtil.HomeActivityNew.onRequestPermissionsResult(HomeActivityNew.java:298)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7404)
at android.app.Activity.dispatchActivityResult(Activity.java:7230)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4477)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4524)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1691)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
编辑:
这是我的代码:
private boolean permissionToRecordAccepted = false;
private boolean permissionToWriteAccepted = false;
private String[] permissions = {"android.permission.RECORD_AUDIO", "android.permission.WRITE_EXTERNAL_STORAGE"};
in(onCreate)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_new);
int requestCode = 200;
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(permissions, requestCode);
}
init();
myHandler = new Handler();
slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN);
SetupToolbar();
SetupPager();
SetupDrawer();
iv_playpause.setTag("pause");
MobileAds.initialize(getApplicationContext(),
getResources().getString(R.string.admov_id));
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
这里是RequestPermissions:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 200:
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
permissionToRecordAccepted=true;
}
if(grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED){
permissionToWriteAccepted=true;
}
break;
}
if (!permissionToRecordAccepted) HomeActivityNew.super.finish();
if (!permissionToWriteAccepted) HomeActivityNew.super.finish();
}
答案 0 :(得分:1)
在所有Android版本上,您的应用需要在其应用清单中声明其所需的正常和危险权限,如声明权限中所述。但是,根据系统版本和应用程序的目标SDK级别,该声明的效果会有所不同:
如果设备运行的是Android 5.1或更低版本,或者您应用的目标SDK为22或更低:如果您在清单中列出了危险权限,则用户必须在安装应用时授予权限;如果他们不授予权限,系统根本不会安装应用程序。 如果设备运行的是Android 6.0或更高版本,并且您应用的目标SDK为23或更高:应用必须列出清单中的权限,并且必须在应用运行时请求其所需的每个危险权限。用户可以授予或拒绝每个权限,即使用户拒绝权限请求,应用程序也可以继续以有限的功能运行。 注意:从Android 6.0(API级别23)开始,用户可以随时撤消任何应用的权限,即使应用程序的API级别较低也是如此。无论您的应用针对什么API级别,您都应该测试自己的应用,以验证其在错过所需权限时是否正常运行。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO);
if (permission != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission to record denied");
makeRequest();
}
}
protected void makeRequest() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO},
RECORD_REQUEST_CODE);
}
Next, implement the onRequestPermissionsResult() method so that it reads as follows:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[]
grantResults) {
switch (requestCode) {
case RECORD_REQUEST_CODE: {
if (grantResults.length == 0
|| grantResults[0] !=
PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission has been denied by user");
} else {
Log.i(TAG, "Permission has been granted by user");
}
return;
}
}
}
答案 1 :(得分:0)
RxPermission是将代码排成1行的最佳库。
然后询问运行时权限。
RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions
.request(Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE) // ask single or multiple permission once
.subscribe(granted -> {
if (granted) {
// All requested permissions are granted
} else {
// At least one permission is denied
}
});
将此库添加到您的build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
}
这不容易吗?