User Permission - Install and Run time Permission
To use sensitive feature or data of Android System or User, application must get permission of doing it. It;s the privacy of Android System User. Depend on system feature, if its not much harmful for user than get it during installation, otherwise application need to get it using popup before using the feature at run time.
We must register permission in manifest file whether it's dangerous or not.
We can use two type of method to implement it. We may use a the default method or a Dexter Library for implementation of dangerous permissions.
Code it for a single permission as As
We must register permission in manifest file whether it's dangerous or not.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codeexpo.alert">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ustb_logo"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SignIn">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SignUp"></activity>
<activity android:name=".ProcessActivity"></activity>
<activity
android:name=".StaffportalActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"></activity>
<activity
android:name=".StudentportalActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
|
We can use two type of method to implement it. We may use a the default method or a Dexter Library for implementation of dangerous permissions.
1. Use Dexter Library
Implement dependencies of Dexter Library and Sync it
dependencies {
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.0'
//noinspection GradleCompatible
implementation 'com.android.support:design:27.1.0'
implementation 'com.karumi:dexter:4.2.0'
}
|
Code it for a single permission as As
Dexter.withActivity(PagerActivity.this)
.withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
//Implement the task here for which this permission is needed
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
// check for permanent denial of permission
if (response.isPermanentlyDenied()) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
|
Code it for a multiple permission as As
void permissionCheck(){
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_SMS
).withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
if (!report.areAllPermissionsGranted()) {
permissionCheck();
}
if (report.isAnyPermissionPermanentlyDenied()) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).onSameThread().check();
}
|
2. Default Method
In the default method, we will Manifest Class as
public class MainActivity extends Activity {
/////////////////////////////////////
String[] permissions = new String[]{
Manifest.permission.CAMERA,
Manifest.permission.CALL_PHONE,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.RECEIVE_MMS,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
};
////////////////////////////////////
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// /////////////////////////
checkPermissions();
}
private boolean checkPermissions() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p : permissions) {
result = ContextCompat.checkSelfPermission(this, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this,
listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 100);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == 100) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else
checkPermissions();
return;
}
}
}
|
Comments
Post a Comment