Created
May 4, 2017 11:55
-
-
Save krunal3kapadiya/ef9683c50e093fa58a482d35d78d8949 to your computer and use it in GitHub Desktop.
Ask multiple runtime permissions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private final int PERMISSION_REQUEST_CODE = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// list of permissions | |
String[] permission = new String[]{ | |
Manifest.permission.CAMERA, | |
Manifest.permission.WRITE_EXTERNAL_STORAGE, | |
Manifest.permission.RECORD_AUDIO}; | |
// checking permissions, if granted or not | |
if ((ContextCompat.checkSelfPermission(mContext, permission[0])) + | |
(ContextCompat.checkSelfPermission(mContext, permission[1])) + | |
(ContextCompat.checkSelfPermission(mContext, permission[2])) | |
!= PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, permission, PERMISSION_REQUEST_CODE); | |
} else { | |
callMethodToRunTask(); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { | |
switch (requestCode) { | |
case PERMISSION_REQUEST_CODE: | |
// If request is cancelled, the result arrays are empty. | |
if (grantResults.length > 0 | |
&& (grantResults[0] + grantResults[1] + grantResults[2]) == PackageManager.PERMISSION_GRANTED) { | |
callMethodToRunTask(); | |
} | |
} | |
} | |
public void callMethodToRunTask() { | |
// implement code | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment