用户在权限屏幕上单击“允许”后启动StartActivity

时间:2017-02-06 16:34:39

标签: java android android-permissions

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

 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();
                            }

4 个答案:

答案 0 :(得分:3)

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),因此您无需创建线程,只需启动Details Activity即可简单的Intent

代码

class StartupActivity extends ..{

    onCreate...(){}

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

}

答案 1 :(得分:1)

是的,您需要覆盖onRequestPermissionsResult

首先在onCreate

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);
}

然后覆盖

  @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSION_ALL) {
                Intent intent = new Intent(StartActivity.this,Details.class);
                startActivity(intent);
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

答案 2 :(得分:0)

@覆盖
   onRequestPermissionsResult(int requestCode,String [] permissions,int [] grantResults) {

// Handle your permission grant/reject scenarios here, 
for( int x = 0; x < grantResults.length; x++ )
{
    if( grantResults[x] == PackageManager.PERMISSION_GRANTED )
    {
         // Since you had one, start your new activity here
    }
    else
    {
        // Do something with the permissions that got BLOCKED??
    }
}

}

一旦用户授予/拒绝许可

,这将由android内部调用

答案 3 :(得分:0)

当您的应用请求权限时,系统会向用户显示一个对话框。当用户响应时,系统会调用您应用的onRequestPermissionsResult()方法,并向其传递用户响应。您的应用必须覆盖该方法才能确定是否已授予权限。回调传递给您传递给requestPermissions()的相同请求代码。例如,如果应用程序请求READ_CONTACTS访问权限,则可能具有以下回调方法:

gem uninstall openssl -v 2.0.3

gem install openssl -v 2.0.2