this project code belongs to the project for the blinds to purchase the items. the scanresult fetch the details to the textview and reads for them and uses gesture options to handle the app.
i created the android project byr barcode scanner and output as textview and voice output. used xzing library as you can see it in the code. the problem is after showing the scanresult in plaintext, it does't atuostart the audio output functionallity. that is the real usage of this project. i referred youtube but can't. then making a chance to anyone to fix this program to text to voice.
this is my code, when working no errors at all. but no audio output after the scanresult from the barcodescanner. i tried my level best. so anyone can help out of it ??/?.
public class SecondActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA =1;
private ZXingScannerView scannerView;
TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
Toast.makeText(SecondActivity.this, "Firebase connection is successful", Toast.LENGTH_LONG).show();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(checkPermission())
{
Toast.makeText(this, "permission is granted", Toast.LENGTH_SHORT).show();
}
else
{
requestPermission();
}
}
}
private boolean checkPermission()
{
return (ContextCompat.checkSelfPermission(SecondActivity.this, CAMERA) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission()
{
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
}
public void onRequestPermissionsResult(int requestCode, String permission[], int grantResults[])
{
switch(requestCode) {
case REQUEST_CAMERA:
if (grantResults.length > 0)
{
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if(cameraAccepted)
{
Toast.makeText(SecondActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(shouldShowRequestPermissionRationale(CAMERA))
{
displayAlertMessage("you need to allow access for both permission",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{CAMERA}, REQUEST_CAMERA);
}
});
return;
}
}
}
}
break;
}
}
#Override
public void onResume()
{
super.onResume();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(checkPermission())
{
if(scannerView == null)
{
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
}
else
{
requestPermission();
}
}
}
#Override
public void onDestroy(){
super.onDestroy();
scannerView.stopCamera();
}
public void displayAlertMessage(String message, DialogInterface.OnClickListener listener)
{
new AlertDialog.Builder(SecondActivity.this)
.setMessage(message)
.setPositiveButton("OK", listener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
EditText ed1;
TextToSpeech txt;
#Override
public void handleResult(Result result) {
final String scanResult = result.getText();
setContentView(R.layout.activity_second);
final TextView text = (TextView) findViewById(R.id.editText);
/* long n=890103073;
long n1=Integer.parseInt(scanResult);
if(n1 == n)
text.setText("Pears Bathing Bar, Pure and Gentle.98% pure Glycerin & Natural Oils. Net weight 75 grams. MRP rupees 105 ");
*/
text.setText(scanResult);
scannerView.stopCamera();
txt=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status!=TextToSpeech.ERROR)
{
txt.setLanguage(Locale.UK);
}
}
});
String toSpeak=ed1.getText().toString();
Toast.makeText(getApplicationContext(),toSpeak,Toast.LENGTH_SHORT).show();
txt.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);
/* public void onPause()
{
if(txt!=null)
{
txt.stop();
txt.shutdown();
}
super.onPause();
}*/
/*AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
scannerView.resumeCameraPreview(SecondActivity.this);
}
});
builder.setNeutralButton("Visit", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse(scanResult));
startActivity(intent);
}
});
builder.setMessage(scanResult);
AlertDialog alert = builder.create();
alert.show();*/
}
}
Related
I want to build an app for quiz using a Speech recognizer it only works for 1 question what if I have a series of questions After each correct/incorrect it moves to the next one, which I already used for the loop here is not working code is not going inside the speechRecognizer.setRecognitionListener(new RecognitionListener() this
if code goes inside it my problem will be solved If anyone knows other methods please answer
public class MainActivity extends AppCompatActivity {
EditText tv;
ImageButton btn;
TextToSpeech textToSpeech;
SpeechRecognizer speechRecognizer;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (EditText) findViewById(R.id.edittext);
btn = (ImageButton) findViewById(R.id.btn);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 1);
}
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.ENGLISH);
}
}
});
btn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("UseCompatLoadingForDrawables")
#Override
public void onClick(View v) {
String[] arr = {"2 multiply 2 is", "4 multiply 4 is ", "3 into 3 is", };
int i=0;
while (i<arr.length ) {
textToSpeech.speak(arr[i], TextToSpeech.QUEUE_FLUSH, null);
try {
Thread.sleep(2000);
speechRecognizer.startListening(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int finalI = i;
speechRecognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float rmsdB) {
}
#Override
public void onBufferReceived(byte[] buffer) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int error) {
}
#Override
public void onResults(Bundle results) {
ArrayList<String> data = results.getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
String res = data.get(0);
tv.setText(res);
if (arr[finalI].equals("2 multiply 2 is")){
if (res.equals("4")) {
textToSpeech.speak("correct", TextToSpeech.QUEUE_FLUSH, null);
speechRecognizer.stopListening();
data.remove(0);
} else {
textToSpeech.speak("Incorrect", TextToSpeech.QUEUE_FLUSH, null);
speechRecognizer.stopListening();
data.remove(0);
}
}
if (arr[finalI].equals("4 multiply 4 is")){
if (res.equals("8")) {
textToSpeech.speak("correct", TextToSpeech.QUEUE_FLUSH, null);
speechRecognizer.stopListening();
data.remove(0);
} else {
textToSpeech.speak("Incorrect", TextToSpeech.QUEUE_FLUSH, null);
speechRecognizer.stopListening();
data.remove(0);
}
}
}
#Override
public void onPartialResults(Bundle partialResults) {
}
#Override
public void onEvent(int eventType, Bundle params) {
}
});
i++; }
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
I have a QR code scanner in my app and the camera stops when my AlertDialog is being displayed but restarts as soon as I move to another activity and keeps running in the background even when the app isn't in the foreground.
I have the stopCamera() and stopCameraPreview() both but it restarts and stays on as long as the app is open (both foreground and background)
Here's the code for the activity:
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView scannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(MainActivity.this, "Scanner Active", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
}
private boolean checkPermission() {
return (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
}
public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA:
if (grantResults.length > 0) {
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted) {
Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(CAMERA)) {
displayAlertMessage("You need to allow access for both permsisions",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{CAMERA}, REQUEST_CAMERA);
}
}
});
return;
}
}
}
break;
}
}
#Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkPermission()) {
if (scannerView == null) {
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
} else {
requestPermission();
}
}
}
#Override
public void onDestroy() {
super.onDestroy();
scannerView.stopCamera();
scannerView.stopCameraPreview();
}
public void displayAlertMessage(String message, DialogInterface.OnClickListener listener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", listener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
String[] a = {"ABCD", "PQR"};
String scanned = "";
#Override
public void handleResult(Result result) {
final String scanResult = result.getText();
for (int i = 0; i < 2; i++) {
if (scanResult.equals(a[i])) {
scanned = a[i];
scannerView.stopCamera();
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("QR Scan");
builder.setMessage("Machine Identified!");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent1 = new Intent(MainActivity.this, buttons.class);
startActivity(intent1);
scannerView.stopCameraPreview();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
scannerView.resumeCameraPreview(MainActivity.this);
}
});
builder.show();
break;
} else {
Toast.makeText(this, "ERROR: Unrecognized QR Code", Toast.LENGTH_LONG).show();
scannerView.resumeCameraPreview(MainActivity.this);
}
}
}
}
"keeps running in the background even when the app isn't in the foreground" - this is due to the Activity lifecycle. Note that onDestroy() is the last method of the class Activity that is called and called only when activity is for sure destroyed.
What it means: when you move to the background onDestroy() is not called. The activity is still alive and waiting to be resumed. Your activity is still alive and it should be. Use instead of onDestroy() method called onPause(). You will reach this method if the activity is killed, and it will return back to the onResume() when appropriate.
Just overwrite onDestroy() to:
#Override
public void onPause() {
super.onPause();
scannerView.stopCamera();
scannerView.stopCameraPreview();
}
I am creating an android application and want to integrate ZXing QR Scanner. When i launch the QRScanner activity from an Intent I get the toast saying "Permission Denied, You cannot access and camera" and the app freezes instead of asking for permission for camera.
Any help to how to correctly implement permission requests or identification of any errors I looked over would be appreciated.
public class Qrscanner extends AppCompatActivity implements
ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView scannerView;
private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
int currentApiVersion = Build.VERSION.SDK_INT;
if(currentApiVersion >= Build.VERSION_CODES.M)
{
if(checkPermission())
{
Toast.makeText(getApplicationContext(), "Permission already granted!", Toast.LENGTH_LONG).show();
}
else
{
requestPermission();
}
}
}
private boolean checkPermission()
{
return (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission()
{
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
}
#Override
public void onResume() {
super.onResume();
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
if(scannerView == null) {
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
} else {
requestPermission();
}
}
}
#Override
public void onDestroy() {
super.onDestroy();
scannerView.stopCamera();
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
final int req = requestCode;
switch (requestCode) {
case REQUEST_CAMERA:
if (grantResults.length > 0) {
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted){
Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(CAMERA)) {
showMessageOKCancel("You need to allow access to both the permissions",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//
requestPermissions(new String[]{CAMERA},
//
REQUEST_CAMERA);
ActivityCompat.requestPermissions(getParent(), new String[]
{Manifest.permission.CAMERA}, req);
}
}
});
return;
}
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new android.support.v7.app.AlertDialog.Builder(Qrscanner.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
#Override
public void handleResult(final Result result) {
final String myResult = result.getText();
Log.d("QRCodeScanner", result.getText());
Log.d("QRCodeScanner", result.getBarcodeFormat().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
scannerView.resumeCameraPreview(Qrscanner.this);
}
});
builder.setNeutralButton("Confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(),AddKeys.class);
intent.putExtra("publicKey",result.getText());
startActivity(intent);
}
});
builder.setMessage(result.getText());
AlertDialog alert1 = builder.create();
alert1.show();
}
}
There is a really simple solution to your problem. Inside the onCreate provide runtime permissions. I suggest you use the dexter library as it's the simplest way of implementing runtime permissions. Here is the link to the library:
https://github.com/Karumi/Dexter
I had the same problem after building your cameSource. Add this line to your code, it works for me
final String[] permissions = new String[] Manifest.permission.CAMERA};
if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(MainActivity.this, permissions, RC_HANDLE_CAMERA_PERM);
}
I think my app enters loop because once it starts in device, it doesn't respond to any commands including lisener the button. I believe that the problem is the methods for permits in RunTime. I'm sorry for my English.
My code is:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final int REQUEST_RESOLVE_ERROR = 3;
private GoogleApiClient mGoogleApiClient;
private volatile Location mCurrentLocation;
private static final int REQUEST_PERMISSION_LOCATE = 2;
private static final int LOCATION_DURATE_TIME = 5000;
private boolean mResolvingError = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(MainActivity.this, "ciao", Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
Toast.makeText(MainActivity.this, "connesso", Toast.LENGTH_LONG).show();
}
#Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
manageLocationPermission();
}
#Override
public void onConnectionSuspended(int i) {
}
//gestione dell'errore
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult)
{
if (mResolvingError) {
// If we're already managing an error we skip the invocation of this method
return;
} else if (connectionResult.hasResolution()) {
// Here we check if the ConnectionResult has already the solution. If it has
// we start the resolution process
try {
// Starting resolution
mResolvingError = true;
// We launch the Intent using a request id
connectionResult.startResolutionForResult(MainActivity.this, REQUEST_RESOLVE_ERROR);
} catch (IntentSender.SendIntentException e) {
// If we have an error during resolution we can start again.
mGoogleApiClient.connect();
}
} else {
// The ConnectionResult in the worse case has the error code we have to manage
// into a Dialog
// Starting resolution
mResolvingError = true;
}
}
//location update
private void updateLocation()
{
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setNumUpdates(1)
.setExpirationDuration(LOCATION_DURATE_TIME);
LocationServices.FusedLocationApi
.requestLocationUpdates(mGoogleApiClient, locationRequest, new LocationListener() {
#Override
public void onLocationChanged(Location location)
{
mCurrentLocation = location;
}
});
}
private void startLocationListener()
{
updateLocation();
}
//permessi in RunTime
private void manageLocationPermission()
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
{
new AlertDialog.Builder(this)
.setTitle("Permesso di geolocalizzazione")
.setMessage("Devi dare il permesso alla geolocalizzazione")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_LOCATE);
}
})
.create()
.show();
}
else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_LOCATE);
}
}else
{
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
manageLocationPermission();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQUEST_PERMISSION_LOCATE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//se mi hanno accettato i permessi
startLocationListener();
}
else{
new AlertDialog.Builder(this)
.setTitle("Permesso di geolocalizzazione")
.setMessage("Devi dare il permesso alla geolocalizzazione")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.create()
.show();
}
}
}
}
If manageLocationPermission() is called with the permissions already granted, your code enters the following else clause
} else {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
manageLocationPermission();
}
Here you're calling the same function again, and since the permissions are granted, you will enter the same else clause, and again the same thing... You see where this is going? Just remove manageLocationPermission() from this else clause
For handling permissions in android M+, I want to write a single class, namely PermissionHandler class, to handle all the permission-related work so that I can easily use the same class in any project without making changes to the calling activity by calling only the constructor:
new PermissionHandler(CallingActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE, new PermissionHandler.PermissionGranted() {
#Override
public void onPermissionGranted() {
doWhatever();
}
});
My PermissionHandler is:
public class PermissionHandler implements ActivityCompat.OnRequestPermissionsResultCallback{
.
.
.
public PermissionHandler(AppCompatActivity callingActivity, String permission, PermissionGranted permissionGranted) {
this.permission = permission;
this.permissionGranted = permissionGranted;
this.callingActivity= callingActivity;
askForPermission();
}
private void askForPermission() {
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
showAlertDialog();
} else {
ActivityCompat.requestPermissions(callingActivity,permissionsArray, PERMISSION_REQUEST);
}
} else {
permissionGranted.onPermissionGranted();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionGranted.onPermissionGranted();
} else {
onPermissionIsNotGranted();
}
break;
}
}
}
My problem here is that onRequestPermissionsResult which is supposed to be invoked when ActivityCompat.requestPermissions(callingActivity,permissionsArray, PERMISSION_REQUEST) is called is never invoked.
What I found out is that this is due to android calling callingActivity.onRequestPermissionsResult which does not exist in the callingActivity and is passed to PermissionHandler.
I also considered using Reflection and Proxies to resolve this issue at runtime, but no success.
As far as i searched i found that its a bug in android. You can see the live issue here. onRequestPermissionsResult() will not be called in any other class then the activity it is called by.
For more details refer this question : onRequestPermissionsResult not being called in dialog fragment. this user having the same problem as yours.
Creating a Common class for requesting permissions is a really good
attempt but my friend, we need to find an alternative for handling
onRequestPermissionsResult()
according Janki Gadhiya answers, I write procedure like
public static WhatYouWould fCustomRequestPermissionsResult(AppCompatActivity appCompatActivity, #NonNull String[] permissions, #NonNull int[] grantResults,...)
in My PermissionHandler class and do what I would, then in for example MainActivity.java, I call that in onRequestPermissionResult :
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
ActiveUtility.CheckPermission.fCustomRequestPermissionsResult(...)
This is what I am using for requesting single permission on Android 11. compileSdk=30, minSdk=30, targetSdk=30
PermittedTask.java
public abstract class PermittedTask {
private ActivityResultLauncher<String> launcher;
private String permission;
private AppCompatActivity activity;
public PermittedTask(AppCompatActivity activity, String permission) {
this.activity = activity;
this.permission = permission;
this.launcher = activity.registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
new ActivityResultCallback<Boolean>() {
#Override
public void onActivityResult(Boolean result) {
if(result) {
granted();
} else {
denied();
}
}
}
);
}
protected abstract void granted();
protected void denied() {}
private void showRequestPermissionRationale() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Permissions needed")
.setMessage("App needs permissions to do that. You can allow or deny in next screen. Proceed?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
launcher.launch(permission);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
denied();
}
})
.show();
}
public void run() {
if(ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED) {
granted();
} else if(activity.shouldShowRequestPermissionRationale(permission)) {
showRequestPermissionRationale();
} else {
launcher.launch(permission);
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
//...
private PermittedTask scanTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
scanTask = new PermittedTask(this, Manifest.permission.ACCESS_FINE_LOCATION) {
#Override
protected void granted() {
startDiscovery();
}
};
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
scanTask.run();
}
});
//...
}
private void startDiscovery() {
//...
}
//...
}
if (checkPermission(PERMISSION_WRITE_EXTERNAL_STORAGE)) {
addToneToStorage();
} else {
requestForPermission(PERMISSION_WRITE_EXTERNAL_STORAGE);
}
BaseActivity
public static final int PERMISSION_RECORD_AUDIO = 1;
public static final int PERMISSION_READ_EXTERNAL_STORAGE = 2;
public static final int PERMISSION_WRITE_EXTERNAL_STORAGE = 3;
public boolean checkPermission(int permission) {
if (ActivityCompat.checkSelfPermission(this, getPermission(permission)) != PackageManager.PERMISSION_GRANTED) {
Debug.e("PERMISSION_CHECK_PERMISSION_FALSE", "-" + permission);
return false;
} else {
Debug.e("PERMISSION_CHECK_PERMISSION_TRUE", "-" + permission);
return true;
}
}
public void requestForPermission(final int permission) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
getPermission(permission))) {
Debug.e("PERMISSION_NEEDED", "-" + permission);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Message
alertDialog.setMessage(getString(R.string.text_permission_rationale));
// Setting Positive "Yes" Button
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(BaseActivity.this, new String[]{getPermission(permission)}, permission);
}
});
alertDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
} else {
Debug.e("PERMISSION_ALLOW", "-" + permission);
ActivityCompat.requestPermissions(this, new String[]{getPermission(permission)}, permission);
}
// END_INCLUDE(camera_permission_request)
}
#Override
public void onRequestPermissionsResult(final int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
Debug.e("PERMISSION_ALLOWED", "-" + requestCode);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Message
alertDialog.setMessage(getString(R.string.text_permission_granted));
// Setting Positive "Yes" Button
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
} else {
Debug.e("PERMISSION_DENIED", "-" + requestCode);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Message
alertDialog.setMessage(getString(R.string.text_permission_not_granted));
// Setting Positive "Yes" Button
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
// Showing Alert Message
alertDialog.show();
}
}
public String getPermission(int permis) {
String permission = null;
switch (permis) {
case PERMISSION_RECORD_AUDIO:
permission = Manifest.permission.RECORD_AUDIO;
Debug.e("PERMISSION", "-" + permission);
return permission;
case PERMISSION_READ_EXTERNAL_STORAGE:
permission = Manifest.permission.READ_EXTERNAL_STORAGE;
Debug.e("PERMISSION", "-" + permission);
return permission;
case PERMISSION_WRITE_EXTERNAL_STORAGE:
permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
Debug.e("PERMISSION", "-" + permission);
return permission;
}
return permission;
}
}