授予所有权限后,应用程序不会转移到下一个活动

时间:2016-10-27 09:18:18

标签: android android-permissions

我正确地允许和拒绝权限并且应用程序第一次正常运行但是当授予所有权限并且下次用户启动应用程序时,它仅显示启动画面并且变得稳定并且不会移动到下一个活动,并且它也没有给出任何错误,我正在尝试下面的内容。

 private static final int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    splashIcon = (ImageView) findViewById(R.id.splashIcon);
    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            Animation anim1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.bounce);
            splashIcon.setAnimation(anim1);
            anim1.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {}
                @Override
                public void onAnimationEnd(Animation animation) {
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                    {
                        checkMultiplePermissions();
                    }else{
                        startActivity(new Intent(Splash.this,UserGuideActivity.class));
                        finish();
                    }
                }
                @Override
                public void onAnimationRepeat(Animation animation)
                {
                }
            });
        }
    }).start();
}
private void checkMultiplePermissions() {
    if (Build.VERSION.SDK_INT >= 23) {
        List<String> permissionsNeeded = new ArrayList<String>();
        List<String> permissionsList = new ArrayList<String>();
        if (!addPermission(permissionsList, android.Manifest.permission.ACCESS_FINE_LOCATION))
        {
            permissionsNeeded.add("GPS");
        }
        if (!addPermission(permissionsList, android.Manifest.permission.READ_EXTERNAL_STORAGE))
        {
            permissionsNeeded.add("Read Storage");
        }
        if(!addPermission(permissionsList, Manifest.permission.READ_SMS))
        {
            permissionsNeeded.add("Read Sms");
        }
        if(!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
        {
            permissionsNeeded.add("Read Contacts");
        }
        if(!addPermission(permissionsList, Manifest.permission.READ_CALL_LOG))
        {
            permissionsNeeded.add("Read Calllogs");
        }

        if (permissionsList.size() > 0)
        {
            requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
        }
    }
}



private boolean addPermission(List<String> permissionsList, String permission) {
    if (Build.VERSION.SDK_INT >= 23)

        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);

            // Check for Rationale Option
            if (!shouldShowRequestPermissionRationale(permission))
                return false;
        }
    return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS: {

            Map<String, Integer> perms = new HashMap<String, Integer>();
            // Initial
            perms.put(android.Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
            perms.put(android.Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_SMS,PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_CONTACTS,PackageManager.PERMISSION_GRANTED);
            perms.put(Manifest.permission.READ_CALL_LOG,PackageManager.PERMISSION_GRANTED);

            // Fill with results
            for (int i = 0; i < permissions.length; i++)
                perms.put(permissions[i], grantResults[i]);
            if (perms.get(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    && perms.get(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                    && perms.get(Manifest.permission.READ_SMS)==PackageManager.PERMISSION_GRANTED
                    && perms.get(Manifest.permission.READ_CONTACTS)==PackageManager.PERMISSION_GRANTED
                    && perms.get(Manifest.permission.READ_CALL_LOG)==PackageManager.PERMISSION_GRANTED)
            {
                // All Permissions Granted
                startActivity(new Intent(this,UserGuideActivity.class));
                finish();
                return;
            } else {
                // Permission Denied
                if (Build.VERSION.SDK_INT >= 23) {
                    Toast.makeText(getApplicationContext(), "Please permit all the permissions", Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        }
        break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

4 个答案:

答案 0 :(得分:4)

因为如果授予所有权限,那么这将是<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><td>one</td><td>two</td></tr> <tr><td>three</td><td>four</td></tr> </table>并且没有false案例可以做任何事情

else

所以应该是这样的

    if (permissionsList.size() > 0)
    {
        requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
    }

改进:您可以通过重复使用 if (permissionsList.size() > 0) { requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); } else{ startActivity(new Intent(Splash.this,UserGuideActivity.class)); finish(); } 函数来消除创建地图和循环,但相应地需要在其他地方进行修改

答案 1 :(得分:1)

检查这个条件:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
    checkMultiplePermissions();
}else{
    startActivity(new Intent(Splash.this,UserGuideActivity.class));
    finish();
}

运行checkMultiplePermissions()后不会发生任何事情,因此只有在授予权限时才需要在那里添加startActivity。

希望这有帮助!

答案 2 :(得分:0)

正如您在问题中所描述的那样,它是第一次运行并且您正在授予所有权限。因此,根据您的代码如果授予所有权限将不会转到 onRequestPermissionsResult。并且您已经在该方法中编写了Intent部分,因此它将坚持使用相同的屏幕。 添加 Pavneet

所述的其他声明

答案 3 :(得分:0)

尝试创建方法以检查是否已授予所有权限(如果未授予),如果您不需要所有权限,它将自动询问每个权限的额外通行isAllPermissionRequired标记以在onRequestPermissionsResult上使用相同的方法移动另一个屏幕

private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 121;

private boolean checkAndRequestPermissions(boolean isAllPermissionRequired) {
        int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        int readStoragePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        int readSmsPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);
        int readContactPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
        int readCallLogPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG);

        List<String> listPermissionsNeeded = new ArrayList<>();
        if (locationPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (readStoragePermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }

        if (readSmsPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_SMS);
        }

        if (readContactPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
        }

        if (readCallLogPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_CALL_LOG);
        }

        if (!listPermissionsNeeded.isEmpty()) {
            if(isAllPermissionRequired) {
                ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
                return false;
            }else {
                return true;
            }
        }
        return true;
    }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS) {
         if (checkAndRequestPermissions(false)) {
             startActivity(new Intent(Splash.this,UserGuideActivity.class));
             finish();
         } 
    }
 }

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   if(checkAndRequestPermissions(true)){
       startActivity(new Intent(Splash.this,UserGuideActivity.class));
       finish();
   }
}

注意:上面的示例我已经从isAllPermissionRequired传递了true = onCreate来请求每个权限isAllPermissionRequired = false来自onRequestPermissionsResult即使用户未授权也不要求任何许可,但如果需要,您需要在isAllPermissionRequired中通过true = onRequestPermissionsResult所有权限。