In my AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
But when I have installed my App-APK, I must setting up the permissions in my Android OS in the settings manually (Settings > Apps > myApp > Permissions > Storage).
How can I get that the permissions setting up automatically. Or the user of my App must confirm it in a dialog?
I use the following code to get the permission in my app
string
<string name="permissions_title">Permissions</string>
<string name="draw_over_permissions_message">To display Audio Widget app needs the permission to draw over another apps.</string>
<string name="read_ext_permissions_message">To load list of music app needs access to your media files.</string>
<string name="btn_continue">Continue</string>
<string name="btn_cancel">Cancel</string>
<string name="toast_permissions_not_granted">Permissions not granted.</string>
java
#TargetApi(Build.VERSION_CODES.M)
private void checkReadStoragePermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, EXT_STORAGE_PERMISSION_REQ_CODE);
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
onPermissionsNotGranted();
}
dialog.dismiss();
finish();
startActivity(getIntent());
}
};
new AlertDialog.Builder(this)
.setTitle(R.string.permissions_title)
.setMessage(R.string.read_ext_permissions_message)
.setPositiveButton(R.string.btn_continue, onClickListener)
.setNegativeButton(R.string.btn_cancel, onClickListener)
.setCancelable(false)
.show();
return;
}
ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.READ_PHONE_STATE}, EXT_STORAGE_PERMISSION_REQ_CODE);
return;
}
}
private void onPermissionsNotGranted() {
Toast.makeText(this, R.string.toast_permissions_not_granted, Toast.LENGTH_SHORT).show();
Log.v("tom", "JERRY");
}
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void checkwriteStoragePermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
Log.v("tom", "TOM");
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
onPermissionsNotGranted();
Log.v("tom", "JERRY");
}
dialog.dismiss();
}
};
new AlertDialog.Builder(this)
.setTitle(R.string.permissions_title)
.setMessage(R.string.read_ext_permissions_message)
.setPositiveButton(R.string.btn_continue, onClickListener)
.setNegativeButton(R.string.btn_cancel, onClickListener)
.setCancelable(false)
.show();
return;
}
ActivityCompat.requestPermissions(ParentActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
return;
}
}
with
private static final int WRITE_EXTERNAL_STORAGE = 4;
private static final int READ_PHONE_STATE = 3;
and you can call the method where ever you want.
Copy given class in your project.
package com.betaiit.helper;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
/**
* Created by Sathish Gadde on 08/05/17.
*/
public class Utility {
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 124;
/**
* Check READ_EXTERNAL_STORAGE Permission
*/
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkPermission_ReadExternalStorage(final Context context) {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("External storage permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}
/**
* Check WRITE_EXTERNAL_STORAGE Permission
*/
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkPermission_WriteExternalStorage(final Context context) {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("External storage permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}
}
Now check permission from your activity :
Utility.checkPermission_ReadExternalStorage(context);
Utility.checkPermission_WriteExternalStorage(context);
Now check permisson from your Fragment :
Utility.checkPermission_ReadExternalStorage(getActivity());
Utility.checkPermission_WriteExternalStorage(getActivity());
I guess you want like this:
i want to give simple solution as :
Add Library in your Gradle file dependencies {} section :
compile 'com.karumi:dexter:4.1.0'
am customize some changes in your code and that changes are very easy to understand.If you have any query,feeling freely to ask me.
private void checkwriteStoragePermission()
{
new android.support.v7.app.AlertDialog.Builder(this)
.setTitle(R.string.permissions_title)
.setMessage(R.string.read_ext_permissions_message)
.setPositiveButton(R.string.btn_continue, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Dexter.withActivity(MultipleBranchesActivity.this)
.withPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
#Override
public void onPermissionGranted(PermissionGrantedResponse response) {
Toast.makeText(MultipleBranchesActivity.this, "Permisson Granted", Toast.LENGTH_SHORT).show();
}
#Override
public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(MultipleBranchesActivity.this, "Permisson Denied", Toast.LENGTH_SHORT).show();
}
#Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();
}
})
.setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setCancelable(false)
.show();
}
and call your method.
Related
Hello I've been struggling with requesting the following bluetooth permissions on Android Studio: BLUETOOTH_CONNECT, BLUETOOTH_SCAN, BLUETOOTH_ADVERTISE but none of them wants to be requested: I'm using the following code to request BLUETOOTH_SCAN permission:
public class MainActivity extends AppCompatActivity {
private final int BLUETOOTH_PERMISSION_CODE = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonRequest = findViewById(R.id.button);
buttonRequest.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.S)
#Override
public void onClick(View v) {
if (
ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED
) {
Toast.makeText(MainActivity.this, "You have already granted this permission!",
Toast.LENGTH_SHORT).show();
} else {
requestStoragePermission();
}
}
});
}
#RequiresApi(api = Build.VERSION_CODES.S)
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.BLUETOOTH_SCAN)) {
new AlertDialog.Builder(this)
.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(MainActivity.this,
new String[]{Manifest.permission.BLUETOOTH_SCAN}, BLUETOOTH_PERMISSION_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_SCAN}, BLUETOOTH_PERMISSION_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == BLUETOOTH_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
}
And there is also a manifest file which contains the following content:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-feature android:name="android.hardware.bluetooth"/>
unfortunately this code doesn't work. It always shows permission denied. It doesn't even display dialog But if I want to request for example READ_EXTERNAL_STORAGE premision it works without any problems could you tell me guys why is this happening? Can't bluetooth permissions be requested by the way from above? Best wishes and waiting for help!
I want create a class called Permissions from below code and then call that in a click of a button. Because there is an #Override method in this activity and I don't know how to override methods inside a class. If I make a class for it the code would be much cleaner and easier to understand.
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
final int REQUEST_CODE_FINE_LOCATION = 1234;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
// we are going to test weather the Location Permission is granted or not
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
textView.setText("Permission Granted...");
} else {
textView.setText("Permission is NOT granted");
}
}
public void requestPermission(View view) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Permission is NOT granted
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
new AlertDialog.Builder(MainActivity.this)
.setMessage("We need permission for fine location")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_FINE_LOCATION);
}
})
.show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_FINE_LOCATION);
}
} else {
// Permission is Granted
textView.setText("Permission Granted");
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_FINE_LOCATION) {
if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission Granted
textView.setText("Permission is Granted");
} else {
//Permission NOT granted
if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
//This block here means PERMANENTLY DENIED PERMISSION
new AlertDialog.Builder(MainActivity.this)
.setMessage("You have permanently denied this permission, go to settings to enable this permission")
.setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
gotoApplicationSettings();
}
})
.setNegativeButton("Cancel", null)
.setCancelable(false)
.show();
} else {
//
textView.setText("Permission NOt granted");
}
}
}
}
private void gotoApplicationSettings() {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", this.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
}
Source: https://github.com/trulymittal/RuntimePermission
Use TedPermission Library which is very easy to use and easy to handle .
Make a Function in separate class and use it anywhere you want
implementation 'gun0912.ted:tedpermission:2.2.3'
public void checkPermissions(Context context) {
PermissionListener permissionlistener = new PermissionListener() {
#Override
public void onPermissionGranted() {
Toast.makeText(context, "Permission Granted", Toast.LENGTH_SHORT).show();
}
#Override
public void onPermissionDenied(List<String> deniedPermissions) {
Toast.makeText(context, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
}
};
TedPermission.with(context)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.check();
}
https://github.com/ParkSangGwon/TedPermission
You can make interface hold all the methods and create abstract class implement the interface then make any classes that inherit from the abstract class read about liskov and segregation principles
I need to implement a QR Code scanner on an android project. I implemented the QR Code, however, I do not know how to switch to another action after QR is read.
I want to create an Intent to move to new action but I do not know where to add it.
I need the app to switch to another action as soon as it detects and reads QR Code.
code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_test);
CodeScannerView scannerViewTest = findViewById(R.id.scanner_view_test);
codeScannerTest = new CodeScanner(this, scannerViewTest);
askPermission();
if(CameraPermission) {
scannerViewTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
codeScannerTest.startPreview();
}
});
codeScannerTest.setDecodeCallback(new DecodeCallback() {
#Override
public void onDecoded(#NonNull Result result) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(ScanTest.this, result.getText(), Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
private void askPermission(){
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(ScanTest.this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERM);
}else {
codeScannerTest.startPreview();
CameraPermission = true;
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == CAMERA_PERM){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
codeScannerTest.startPreview();
CameraPermission = true;
}else{
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){
new AlertDialog.Builder(this)
.setTitle("Permission")
.setMessage("Please provide the camera permission for using all the features of the app")
.setPositiveButton("Proceed", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(ScanTest.this, new String[]{ Manifest.permission.CAMERA}, CAMERA_PERM);
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
} else {
new AlertDialog.Builder(this)
.setTitle("Permission")
.setMessage("You have denied some permissions. Allow all permissions at [Settings] > [Permissions]")
.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent settings_intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null));
settings_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(settings_intent);
finish();
}
}).setNegativeButton("No, Exit app", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).create().show();
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
#Override
protected void onPause() {
if(CameraPermission){
codeScannerTest.releaseResources();
}
super.onPause();
}
You need to add it at the onDecoded method
codeScannerTest.setDecodeCallback(new DecodeCallback() {
#Override
public void onDecoded(#NonNull Result result) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Intent intent = Intent(this, YouNextActivity.class);
intent.putExtra("barcode", result.getText());
startActivity(intent);
//Toast.makeText(ScanTest.this, result.getText(), Toast.LENGTH_SHORT).show();
}
});
}
});
You can use the Intent.getExtra("barcode") and can get the value in your next Activity.
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.
i have method for get runtime permission i searched for that long but still have no answer can anyone help me to modify my code ? cause its never show request dialog for get user permission after user accept permission move to next activity
here my code that i made but some how its never made a request
public class perm extends AppCompatActivity {
private static final int REQUEST_PERMISSION_SETTING = 200;
private View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dash);
isPermissionGranted();
}
public boolean isPermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.CALL_PHONE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("TAG","Permission is granted");
return true;
} else {
Log.v("TAG","Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PERMISSION_SETTING);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v("TAG","Permission is granted");
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(permissions.length == 0){
return;
}
boolean allPermissionsGranted = true;
if(grantResults.length>0){
for(int grantResult: grantResults){
if(grantResult != PackageManager.PERMISSION_GRANTED){
allPermissionsGranted = false;
break;
}
}
}
if(!allPermissionsGranted){
boolean somePermissionsForeverDenied = false;
for(String permission: permissions){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
//denied
Log.e("denied", permission);
}else{
if(ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED){
//allowed
Log.e("allowed", permission);
} else{
//set to never ask again
Log.e("set to never ask again", permission);
somePermissionsForeverDenied = true;
}
}
}
if(somePermissionsForeverDenied){
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Permissions Required")
.setMessage("You have forcefully denied some of the required permissions " +
"for this action. Please open settings, go to permissions and allow them.")
.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setCancelable(false)
.create()
.show();
}
} else {
switch (requestCode) {
//act according to the request code used while requesting the permission(s).
}
}
}
}