gpt4 book ai didi

java - StartActivity 一旦用户在权限屏幕上单击允许

转载 作者:搜寻专家 更新时间:2023-11-01 07:46:59 25 4
gpt4 key购买 nike

我的一项 Activity 需要位置许可,我写了下面的代码来获得许可。但在这种情况下,如果应用最初没有位置权限,用户需要点击两次才能打开 Activity 。我能否进行一些更改,以便一旦用户在“权限”屏幕上单击“允许”,然后才会触发 intent。

 int PERMISSION_ALL = 1;
String[] PERMISSIONS = new String[0];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PERMISSIONS = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION};
}

if (!hasPermissions(getApplicationContext(), PERMISSIONS)) {
ActivityCompat.requestPermissions(StartupActivity.this, PERMISSIONS, PERMISSION_ALL);
}

if (hasPermissions(getApplicationContext(), PERMISSIONS)) {
new Thread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(StartActivity.this, Details.class);
startActivity(intent);
}
}).start();
}

最佳答案

override onRequestPermissionsResult 如果权限被授予,则从那里开始你的Activity

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == PERMISSION_ALL){
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED ){

new Thread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(StartActivity.this, Details.class);
startActivity(intent);
}
}).start();
}else{
Toast.makeText(MainActivity.this, "Access Denied ! Cannot proceed further ", Toast.LENGTH_SHORT).show();
}
}
}

注意:显然,代码似乎在 StartupActivity 中(来自 StartupActivity.this),因此您不需要创建线程,只需启动您的 详细信息 Activity 带有简单的Intent


代码将是

class StartupActivity extends ..{

onCreate...(){}

// here you will have onRequestPermissionsResult
@Override
public void onRequestPermissionsResult( ....){...}

}

关于java - StartActivity 一旦用户在权限屏幕上单击允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42072770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com