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
set SILENT mode to Normal mode using AudioManager
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
Log.i("MyApp","Silent mode");
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
break;
}
AudioManager manager = (AudioManager) getContext().getSystemService(AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
try this:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
To make it work for android devices for and above Marshmallow(API 23)
In AndroidManifest.xml
define the ACCESS_NOTIFICATION_POLICY
permission
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
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);
}