I am making application where phone mode will changes from silent mode to ringer mode if the user sends an sms with some specific keyword. The application mainly works when
To make it work for android devices for and above Marshmallow(API 23)
In AndroidManifest.xml
define the ACCESS_NOTIFICATION_POLICY
permission
In your onResume/onCreate
function ask for permission if not granted
@Override
protected void onResume() {
super.onResume();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//Check if the phone is running Marshmallow or above
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
//If the permission is not granted, launch an inbuilt activity to grant permission
if (!nm.isNotificationPolicyAccessGranted()) {
startActivity(new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS));
}
}
}
Finally to change Ringer mode call this method
public void setRingerMode(Context context,int mode){
/*
* mode variable value can be:
* AudioManager.RINGER_MODE_SILENT
* AudioManager.RINGER_MODE_NORMAL
* AudioManager.RINGER_MODE_VIBRATE
*/
NotificationManager nm = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
AudioManager audioManager = (AudioManager)context.getSystemService(AUDIO_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M && nm.isNotificationPolicyAccessGranted())
audioManager.setRingerMode(mode);
}