Permission send sms android - android

I am writing an app that will send a message to an inputted number through SMS. However when I try to send the message I get the error that "User 10074 does not have android.permission.SEND_SMS" even though I have this permission in my Manifest.
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("5554", null, "hello", null, null);
// smsManager.sendTextMessage(number,null,matn,null,null);
Toast.makeText(Sms.this, "OK", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(Sms.this, "Error"+e, Toast.LENGTH_LONG).show();
}
}
});
and code in manifest
<uses-permission android:name="android.permission.SEND_SMS" />

Please try below code with runtime permission.
call checkAndroidVersion("5554"); from your click listener
public void checkAndroidVersion(String mobile){
this.mobile= mobile;
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(RegistrationActivity.this,Manifest.permission.SEND_SMS);
if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(RegistrationActivity.this,new String[]{Manifest.permission.SEND_SMS},SEND_SMS);
return;
}else{
sendSms(mobile);
}
} else {
sendSms(mobile);
}
}
private void sendSms(String mobileNo){
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mobileNo, null, "hello", null, null);
// smsManager.sendTextMessage(number,null,matn,null,null);
Toast.makeText(Sms.this, "OK", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(Sms.this, "Error"+e, Toast.LENGTH_LONG).show();
}
}
Also Override onRequestPermissionsResult method
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case SEND_SMS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendSms(mobile);
} else {
Toast.makeText(Sms.this, "SEND_SMS Denied", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
try this to ask run time permission
requestSmsPermission();
private void requestSmsPermission() {
String permission = Manifest.permission.READ_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
}
#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(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
}
}
}

Related

Runtime Permissions not working

This is my code in OnCreate() method :
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.RECIEVE_SMS)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},1);
}
And in my onRequestPermissionsResult() method :
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
init();
}else{
finish();
}
And my Manifest file has the following permissions
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
But i'm not getting the dialog asking for the permission and i can't figure out the problem?
if your app targeting Android 6.0 and above than add runtime permission
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app
add runtime permission using below code for READ_SMS
String permission = Manifest.permission.READ_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
and than handle result like this
#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(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();
// perform your action here
} else {
Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
Try this:
if (ActivityCompat.checkSelfPermission(AddnewPhotoActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AddnewPhotoActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);
} else {
isPermissionGranted(true);
}
isPermissionGranted(boolean); ====> Method call
public void isPermissionGranted(boolean permission) {
if (permission) {
//Your Operation
} else {
Toast.makeText(this, "Permission not granted", Toast.LENGTH_SHORT).show();
}
}
onRequestPermissionsResult
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_READ_EXTERNAL_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
isPermissionGranted(true);
} else
{
isPermissionGranted(false);
}
}
}

Android runtime permission is GRANTED but still denied

I'm trying to write an app that sends SMS. When checking if I have the required permissions it returns true but still crashes with SecurityException.
When button is pressed
private void startAutoMsg() {
Log.d("Starting Auto Msg");
//FIXME: Start proper loop
if (canSendSms()) {
sendMessage();
mAutoMsgButton.setBackgroundColor(Color.GREEN);
}
}
I'm using the below function to determine if I have the proper permission
private boolean canSendSms() {
if ((ContextCompat.checkSelfPermission(mContext, Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED)) {
Log.d("Permission granted");
return true;
} else {
Log.d("Permission denied");
ActivityCompat.requestPermissions(
mActivity, new String[]{Manifest.permission.SEND_SMS}, 101);
return false;
}
}
The above code returns true and therefore SMS is trying to be sent with this
private void sendMessage() {
Log.d("sending message");
PendingIntent sentPI = PendingIntent.getBroadcast(
mContext, 0, new Intent(Constants.ACTION_SMS_SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(
mContext, 0, new Intent(Constants.ACTION_SMS_DELIVERED), 0);
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
mSmsManager.sendTextMessage("mNumber", null, mText, sentPI, deliveredPI);
}
});
thread.run();
}
In my manifest:
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
Log from crash
D/SmsApp: TelephonyFragment: canSendSms(533): Permission granted
D/SmsApp: TelephonyFragment: sendMessage(222): sending message
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.phone, PID: 16736
java.lang.SecurityException: Sending SMS message: uid 1001 does not have android.permission.SEND_SMS.
at android.app.ContextImpl.enforce(ContextImpl.java:1727)
at android.app.ContextImpl.enforceCallingPermission(ContextImpl.java:1749)
at android.content.ContextWrapper.enforceCallingPermission(ContextWrapper.java:750)
at android.content.ContextWrapper.enforceCallingPermission(ContextWrapper.java:750)
at com.android.internal.telephony.IccSmsInterfaceManager.sendText(IccSmsInterfaceManager.java:410)
at com.android.internal.telephony.UiccSmsController.sendTextForSubscriber(UiccSmsController.java:136)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:366)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:349)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment$2.run(TelephonyFragment.java:230)
at java.lang.Thread.run(Thread.java:764)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.sendMessage(TelephonyFragment.java:233)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.startAutoMsg(TelephonyFragment.java:517)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.toggleAutoMsg(TelephonyFragment.java:507)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.lambda$-com_rawinc_smsapp_ui_telephony_TelephonyFragment_11555(TelephonyFragment.java:359)
at com.rawinc.smsapp.ui.telephony.-$Lambda$uKVldJdEkN_fZa3QWm3EZHDa2r8$2.$m$0(Unknown Source:4)
at com.rawinc.smsapp.ui.telephony.-$Lambda$uKVldJdEkN_fZa3QWm3EZHDa2r8$2.onClick(Unknown Source:0)
at android.view.View.performClick(View.java:6178)
at android.view.View$PerformClick.run(View.java:24416)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:255)
at android.app.ActivityThread.main(ActivityThread.java:6555)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Please Do This :
Create This Class in your App:
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
public abstract class RuntimePermissionsActivity extends AppCompatActivity
{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int permissionCheck = PackageManager.PERMISSION_GRANTED;
for (int permission : grantResults)
{
permissionCheck = permissionCheck + permission;
}
if ((grantResults.length > 0) && permissionCheck == PackageManager.PERMISSION_GRANTED)
{
onPermissionsGranted(requestCode);
}
else
{
onPermissionsDeny(requestCode);
}
}
public void requestAppPermissions(final String[] requestedPermissions, final int requestCode)
{
int permissionCheck = PackageManager.PERMISSION_GRANTED;
boolean shouldShowRequestPermissionRationale = false;
for (String permission : requestedPermissions)
{
permissionCheck = permissionCheck + ContextCompat.checkSelfPermission(this, permission);
shouldShowRequestPermissionRationale = shouldShowRequestPermissionRationale || ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
}
if (permissionCheck != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, requestedPermissions, requestCode);
}
else
{
onPermissionsGranted(requestCode);
}
}
public abstract void onPermissionsGranted(int requestCode);
public abstract void onPermissionsDeny(int requestCode);
}
Next in your MainActivity (or any Activity) You Shoulde extends From This Class like This :
public class Main_Activity extends RuntimePermissionsActivity
And Overrite This Method in your Activity :
#Override
public void onPermissionsGranted(int requestCode) {
}
#Override
public void onPermissionsDeny(int requestCode) {
}
In Final : Use this Code To Check Permmision :
MainActivity.super.requestAppPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestcodeWriteStorage);
Also you can Check many Permission and you shoulde define in String Array ;
requestcodeWriteStorage is a int you should define it ;
Manage your Callback with 2 method that i read .
for example :
#Override
public void onPermissionsGranted(int requestCode) {
if (requestCode == requestcodeWriteStorage){
Toast.makeText(this,"Done",Toast.LENGTH_LONG).show();
}
Check permission when need location
if ((ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{Manifest.permission.SEND_SMS},
101);
}
This lines adds ur activity
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_CODE:
if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
You need to handle onRequestPermissionResult
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.v("======>","Permission: "+permissions[0]+ "was "+grantResults[0]);
//your task
}
}
and check for version
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED) {
Log.v("======>","Permission is granted");
return true;
} else {
Log.v("======>","Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v("======>","Permission is granted");
return true;
}
and to send message use
private void sendMessage() {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, "TXt", null, null);
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
On 5.0 and higher u would have to also grant your app the permission to read sms in settings.
Make sure you have used android Android 6.0 and higher SDK for your app, I had the same issue and I found that I used android 4.1 kitkat sdk, once I change the min sdk level to 21, problem solved

Camera permission is not working on Marshmallow and above devices and crashes

Since I have made a flashlight application and it works properly on the device which have SDK 22 and below. But when I come to check on marshmallow and above devices, it doesn't run and crashes at the beginning only I asked for permission using following code but it doesn't seems to be working at all. here is my code for requesting permission of camera at run time. here is my code:
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 5) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// if permission not granted it will force close the app
}
}
I have tried in marshmallow, nougat devices but it doesnt ask for camera permission and i have to go through manually from setting-app-flashlight-permission-allow. can anyone help me please. Currently I am testing my app in Lineage os 7.1.1
You need to add the SDK 23 permission in manifest. add the below line in manifest then you will get runtime permission callback.
<uses-permission-sdk-23 android:name="android.permission.CAMERA"/>
Just you change only if condition your code is perfect like this,
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
to
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if( ContextCompat.checkSelfPermission(this,
android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
if(currentAPIVersion>= 23)
{
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
Manifest.permission.CAMERA)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Camera Permission Necessary");
alertBuilder.setMessage("Camera permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
return false;
} else {
return true;
}
} else {
return true;
}
Use the below code and call the "requestPermission" method in onCreate of the Activity:
private static final int REQUEST_CODE_PERMISSION = 2;
List<String> mPermission=new ArrayList<String>();
public void requestPermission()
{
try {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= MockPackageManager.PERMISSION_GRANTED)
mPermission.add(Manifest.permission.CAMERA);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= MockPackageManager.PERMISSION_GRANTED
)
mPermission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE
);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= MockPackageManager.PERMISSION_GRANTED)
mPermission.add(Manifest.permission.READ_EXTERNAL_STORAGE);
if(mPermission.size()>0)
{
String[] array = mPermission.toArray(new String[mPermission.size()]);
ActivityCompat.requestPermissions(this, array, REQUEST_CODE_PERMISSION);
// If any permission aboe not allowed by user, this condition will execute every tim, else your else part will work
}
} catch (Exception e) {
e.printStackTrace();
}
}
Overwrite the method "onRequestPermissionsResult":
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.e("Req Code", "" + requestCode);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (grantResults.length == mPermission.size()) {
for(int i=0;i<grantResults.length;i++)
{
if(grantResults[i] == MockPackageManager.PERMISSION_GRANTED)
{
//don't do anything....
}
else{
mPermission=new ArrayList<String>();
requestPermission();
break;
}
}
}
else{
Toast.makeText(this,"permition not granted",Toast.LENGTH_LONG).show();
mPermission=new ArrayList<String>();
requestPermission();
}
}
}
do this way:
if (ContextCompat.checkSelfPermission(UserProfileActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(UserProfileActivity.this, new String[]{Manifest.permission.CAMERA},
5);
} else {
//start flashlight
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String permissions[], #NonNull int[] grantResults) {
switch (requestCode) {
case 5: {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
//start flashlight
Log.d("Permissions CAMERA", "Permission Granted: " + permissions[i]);
} else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
Log.d("Permissions CAMERA", "Permission Denied: " + permissions[i]);
}
}
break;
}
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
} break;
}
}
I also had the same problem but managed to fixed it. Here's my case (I don't know if its the same as yours but it's worth a try)
So I have this two activities namely: MainActivity, and FlashlightActivity.
I used to ask for camera permission explicitly in FlaslightActivity just before I tap the switch button but I forgot that I was checking if a user's phone has a camera in the onCreate() method. while I was asking for permission onClick of the switch button of the flashlight, in which that was after I checked if a user's phone has a camera. So what I did was to ask for the permission in my MainActivity just before I start FlashlightActivity.
flashBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(hasPermission()){
Intent flashIntent = new Intent("FlashlightActivity");
startActivity(flashIntent);
}else{
requestPermission();
}
}
});
So basically ask for the permission before you call any Camera related stuffs

onReqestPermissionResult not being called in android dynamic permission

i am trying to invoke the user to enable their write external storage permission at run time. i have used the below code. it shows the run time permission dialog. when i click allow or deny, it will doesn't call the onReqestPermissionResult (call back method) method.
what i have to do for that?
public class PermissionCheck extends Activity {
private static final String TAG = "PermissionCheck";
private Fragment mFragment;
private Activity mActivity;
public PermissionCheck(Fragment fragment){
mFragment = fragment;
mActivity = fragment.getActivity();
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
return true;
} else {
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else {
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
}
}
}
You are passing another activity's instance in requestPermissions, so the onRequestPermissionResult is called in your mActivity and not in the PermissionCheck. Try changing requestPermissions to this :
ActivityCompat.requestPermissions(PermissionCheck.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Replace:
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
With :
ActivityCompat.requestPermissions(PermissionCheck.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
And replace onRequestPermissionsResult() to this:
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getActivity(), "Permission is granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show();
}
break;
}
}
}

Not able to get Android run time permission result

I want to check SMS reading permission is Granted or not in API 23+. So I implemented its as follows;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}
Handling permission resp. as follows;
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_SMS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Doing task regarding SMS permission.
}else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_SMS)) {
//Show an explanation to the user *asynchronously*
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This permission is important to Read SMS.")
.setTitle("Important permission required");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ActivityCompat.requestPermissions(GenerateOTPActivity.this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}
});
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}else{
//Never ask again and handle your app without permission.
}
}
}
}
By default SMS permission is Granted so checkSelfPermission() reterns zero but when I manually deny permission from device setting then also checkSelfPermission() returns zero value.
I dont understand how to check SMS permission is denied or not. Please provide me some solutions.
try this codes and also provide permissions in android manifest
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
marshmallowPermission(); //***** marshmaloow runtime permission*****//
}
public Boolean marshmallowPermission(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(this,"Permission already granted.", Toast.LENGTH_SHORT).show();
} else {
requestPermission();
}
}
return true;
}
#TargetApi(Build.VERSION_CODES.M)
public boolean checkPermission(){
int sms = checkSelfPermission(android.Manifest.permission.READ_SMS);
if (sms == PackageManager.PERMISSION_GRANTED ){
return true;
} else {
return false;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void requestPermission(){
if (shouldShowRequestPermissionRationale(android.Manifest.permission.READ_SMS)){
if(shouldShowRequestPermissionRationale(android.Manifest.permission.READ_SMS)){
Toast.makeText(this,"sms Permission must be needed which allows to access sms . Please allow sms in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
}
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", this.getPackageName(), null);
intent.setData(uri);
this.startActivity(intent);
} else {
requestPermissions(new String[]{android.Manifest.permission.READ_SMS},100);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 100:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,"Permission Granted, Now you can access app without bug.",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,"Permission Denied, App maybe get crashed.", Toast.LENGTH_LONG).show();
}
break;
}
}

Categories

Resources