我正在尝试实现SplashScreenActivity,它将请求所有必要的权限,然后重定向到MainActivity:
public class SplashScreenActivity extends Activity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.splash_screen);
try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
((TextView) findViewById(R.id.versionView)).setText(info.versionName);
} catch (Exception e) {
throw new IllegalStateException(e);
}
if (ActivityCompat.checkSelfPermission(this, CAMERA) != PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{READ_PHONE_STATE, CAMERA}, 200);
} else {
onPermissionsReady();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 200) {
onPermissionsReady();
}
}
private void onPermissionsReady() {
new Fork() {
@Override public void run() {
ApplicationContext.getInstance(SplashScreenActivity.this);
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
}
};
}
}
我有两个问题:
onRequestPermissionsResult
并且应用程序结束。编辑:我在这里创建了一个示例应用程序:https://github.com/knyttl/TestApp - 它演示了两个问题。
EDIT2 :当我同意/不同意权限请求时会发生这种情况 - 应用程序刚刚结束:https://www.youtube.com/watch?v=lhvhXcEJxLw&feature=youtu.be
答案 0 :(得分:0)
AppCompatActivity
延伸。尝试更改该行:
if (ActivityCompat.checkSelfPermission(this, CAMERA) != PERMISSION_GRANTED
到
`if (ActivityCompat.checkSelfPermission(this, Manifest.Permission.CAMERA) != PERMISSION_GRANTED`
onResume
。或者将其保留在onCreate
但延迟,例如Handler
答案 1 :(得分:0)
我发现了问题:活动有noHistory = true,导致杀死应用程序,如下所述:
Requesting Android M permissions from Activity with noHistory="true" and/or showOnLockScreen="true"