我一直在Android Q上测试我的应用程序,发现奇怪的行为使应用程序无法运行。然后,我可以用最少的代码在一个新项目中重现它。
问题在于活动处于重新创建的无限循环中(一遍又一遍地调用onCreate)。这是通过在运行时请求权限来触发的。即使已经授予权限并且没有显示请求UI,也会发生这种情况。
这仅发生在Q上,而不发生在Pie或Oreo或棉花糖上。我不知道我的代码是否做得很奇怪,还是当前Q仿真器映像中的OS错误。你怎么看?在不触发此错误的情况下,我还能如何请求启动应用程序的权限?
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.a.myapplication">
<uses-feature
android:name="android.hardware.location"
android:required="false" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.a.myapplication"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
MainActivity.java:
package com.a.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("Activity", "Activity created!");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
requestPermissionsOnStartup();
}
}, 1500);
}
private void requestPermissionsOnStartup() {
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
},
25547);
}
}
这是启动此应用程序后在logcat中看到的内容:
2019-07-09 11:10:03.234 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:03.326 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:03.444 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:03.577 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:03.728 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:03.864 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:03.987 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:04.127 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:04.254 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:04.371 7454-7454/com.a.myapplication E/Activity: Activity created!
2019-07-09 11:10:04.507 7454-7454/com.a.myapplication E/Activity: Activity created!
这些是我确切的模拟器设置: