简体   繁体   中英

Whenever I launch the app for the first time the app crashes and then it asks me the user permission

My Android application crashes the first time launching and it asks me about the user call permission. When I launch my app again then it works normally. I have tried the following code.

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

Your code is right but you set the codes on wrong places lets try this it will definitely help you....

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

Permission method.....

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

after that make java class for getting permission....

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

}

The main thing you have to remember is to put your working method at the right place like my is filter();

It will work for you because it work for me when i stuck in the same situation

Add the following code

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

Call this Method from your on create

checkAndRequestPermissions();

Add the following method to your Main Activity

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

I just solved an exception. I did two things. Firstly, I removed the initialization of Contacts by
Contacts = new ArrayList<ContactModel> from the loadContactList() and added it into the onCreate() and Secondly I just changed the return datatype of loadContactList() from void to list. Then I returned the contacts in loadContacts() so that loadListView() can fetch this list and display it into the list view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM