每当我第一次启动该应用程序时,该应用程序崩溃,然后它会询问我用户权限

时间:2019-01-22 05:17:56

标签: java android permissions call

我的Android应用程序在首次启动时崩溃,它询问我有关用户呼叫权限的信息。当我再次启动我的应用程序时,它可以正常工作。我已经尝试了以下代码。

public void loadContactList() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
    } else {.....}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) {
    if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            loadContactList();
        } else {
            Toast.makeText(this, "Until you grant the permission, we canot display the list", Toast.LENGTH_SHORT).show();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您的代码是正确的,但是您将代码设置在错误的位置,请尝试使用它肯定会为您提供帮助。...

In Your Main Activity...

private int STORAGE_PERMISSION_CODE = 1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == 
  PackageManager.PERMISSION_GRANTED) {
        filter();
        Toast.makeText(this, ""+arrayList, Toast.LENGTH_SHORT).show();
    } else {
        Permission.requestStoragePermission(MainActivity.this,
                  STORAGE_PERMISSION_CODE);
    }

}//on create closed

权限方法.....

//---------------------------------RuntimePermission-----------------------------//
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
    permissions, @NonNull int[] grantResults) {
    if (requestCode == STORAGE_PERMISSION_CODE)  {
        if (grantResults.length > 0 && grantResults[0] == 
     PackageManager.PERMISSION_GRANTED) {
            filter();
        } else {
            Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();

Permission.requestStoragePermission(MainActivity.this,STORAGE_PERMISSION_CODE);
        }
    }
}

之后,使Java类获得许可。...

public class Permission {
public static void requestStoragePermission(final Activity activity, final int 
       STORAGE_PERMISSION_CODE) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        new AlertDialog.Builder(activity)
                .setTitle("Permission needed")
                .setMessage("This permission is needed because of this and that")
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(activity,
                                new String[] 
        {Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create().show();
    } else {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
   STORAGE_PERMISSION_CODE);
    }
}

}

您要记住的主要事情是将工作方法放在正确的位置,例如my is filter();。

它会为您服务,因为当我遇到同样的情况时,它会为我服务

答案 1 :(得分:0)

添加以下代码

String[] appPermissions =
            {Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_CONTACTS};
    int PERMISSIONS_REQUEST_CODE = 234;

从创建时就调用此方法

checkAndRequestPermissions();

在您的主要活动中添加以下方法

public boolean checkAndRequestPermissions() {
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String perm : appPermissions) {
            if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(perm);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), PERMISSIONS_REQUEST_CODE);
            return false;
        }
        return true;
    }

答案 2 :(得分:0)

我刚刚解决了一个例外。我做了两件事。首先,我删除了
通讯录的初始化 Contacts = new ArrayList<ContactModel>中的loadContactList()并将其添加到onCreate()中,然后我刚刚将loadContactList()的返回数据类型从void更改为list。然后,我在loadContacts()中返回了联系人,以便loadListView()可以获取此列表并将其显示在列表视图中。