Monday, 20 February 2017


Permissions to send SMS in Android Marshmallow (Api 23) and above.



public class commonUtil{

public static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 100;

public static void checkSMSPermission(Context context) {

if (ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
Manifest.permission.SEND_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.

ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
// MY_PERMISSIONS_REQUEST_SEND_SMS is an
// app-defined int constant. The callback method gets the
// result of the request.

}
} else {
}
}
}


In your Activity or Fragment.



public void checkPermission{

if (Build.VERSION.SDK_INT >= 23){

if (permission granted){
SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(destinationAdrs, null, msg, null, null);
}else {
CommonUtils.checkSMSPermission(getActivity());
}
}else {
SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(destinationAdrs, null, msg, null, null);
}
}

No comments:

Post a Comment