暂停启动画面,直到用户响应 android 应用中的权限请求

Pause splash screen until user responds to permission request in android app

在我的 android 应用程序中,我有一个启动画面,在这 5 秒内持续 5 秒 我要求用户授予位置权限,我想暂停启动画面 activity 直到用户授予或拒绝权限 request.If activity 在用户响应之前不会暂停,下一个 activity 将在没有用户对权限请求的响应的情况下被激活这是我的代码

SpalshScreen activity:

public class SplashScreen extends BaseActivity {

    private static int SPLASH_SCREEN_TIME_OUT = 6000;

    some properties ....


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title
        getSupportActionBar().hide(); //hide the title bar
        setContentView(R.layout.activity_splash_screen);

       // This is the method that asks the user for permission
        locationPermGranted = CheckLocationPermission();


        deviceLanguage = Locale.getDefault().getDisplayLanguage();
        DeviceLanguageCode = Locale.getDefault().getLanguage();
        sharedPreferences = getSharedPreferences(getPackageName() + ".prefs", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        codeIntent = new Intent(SplashScreen.this, PhoneVerification.class);
        myIntent = new Intent(SplashScreen.this, ProfileSettings.class);
        loggedIntent = new Intent(SplashScreen.this, Main.class);
        notLoggedIntent = new Intent(SplashScreen.this, Login.class);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                if (!locationPermGranted) {

                    killApp();
                }

                if (sharedPreferences.getString("userLanguage", DeviceLanguageCode) == null) {
                    editor.apply();
                }

                if (sharedPreferences.getBoolean("isRegistered", false)) {


                    if (sharedPreferences.getBoolean("isPhoneVerified", false)) {


                        if (sharedPreferences.getBoolean("isLoggedIn", false)) {
                            // GO TO HOME

                            startActivity(loggedIntent);
                        } else {
                            // GO TO LOGIN
                            editor.apply();
                            startActivity(notLoggedIntent);
                        }

                    } else {
                        editor.apply();
                        startActivity(codeIntent);
                    }
                } else {
                    //GO TO PROFILE SETTINGS
                    editor.apply();
                    startActivity(myIntent);
                }

                finish();

            }
        }, SPLASH_SCREEN_TIME_OUT);

    } 

用 if-else 语句包围 newHandler().postDelayed 方法。如果用户授予权限,则仅 运行 post 延迟方法。

预先检查权限,如果已经授予权限,则继续您的Handler(),如果没有,则请求权限并处理结果。方法如下:

  1. 将所有 Handler() 代码放在一个函数中,这样您就可以轻松调用它:

    private void continueOperation(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
    
                if (sharedPreferences.getString("userLanguage", DeviceLanguageCode) == null) {
                    editor.apply();
                }
    
                if (sharedPreferences.getBoolean("isRegistered", false)) {
    
    
                    if (sharedPreferences.getBoolean("isPhoneVerified", false)) {
    
    
                        if (sharedPreferences.getBoolean("isLoggedIn", false)) {
                            // GO TO HOME
    
                            startActivity(loggedIntent);
                        } else {
                            // GO TO LOGIN
                            editor.apply();
                            startActivity(notLoggedIntent);
                        }
    
                    } else {
                        editor.apply();
                        startActivity(codeIntent);
                    }
                } else {
                    //GO TO PROFILE SETTINGS
                    editor.apply();
                    startActivity(myIntent);
                }
    
                finish();
    
            }
        }, SPLASH_SCREEN_TIME_OUT);
    }
    

    在这里,你不需要检查权限,因为这个函数只会被调用 已获得许可。

  2. 然后,在onCreate()中进行权限检查:

    if (!locationPermGranted)
        killApp();
    else
        continueOperation();
    
  3. 现在,处理 onRequestPermissionResult() 并在获得许可后调用 continueOperation()

    如果在 onRequestPermissionResult() 中授予权限,您不希望用户等待 6 秒,这显然是糟糕的 UX,您可以在 else{} 第二点。因此,如果先前已授予权限,则用户只需等待,这是 SplashScreen 的基本目的。