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);
}
}
Permissions to make a call in Android Marshmallow (Api 23) and above.

public class CommonUtils {

private static final int REQ_CODE = 100;

public void checkCallPermission(String number){

//use ACTION_CALL for callsIntent callIntent = new Intent(Intent.ACTION_CALL);
//here number is desired phone number you wish to call.callIntent.setData(Uri.parse("tel:"+number));

 //check the permissions, if the device is running Android 6.0 and the applicaiton targetSdkVersion is 23 or above, asks the user to grant permission.

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
 //request permission from user, if the app didn't get the required permission ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CALL_PHONE}, REQ_CODE );
 return;
 }else {
// if device has permissions
try{
//start activity and make phone call
startActivity(callIntent);
}catch (android.content.ActivityNotFoundException ex){ Toast.makeText(getActivity(),""+ex.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}
}