如果用户发送带有某些特定关键字的短信,我正在进行应用,其中手机模式将从静音模式更改为振铃模式。该应用程序主要用于用户在室内某处丢失其单元格并且处于静音模式时。 我的主要问题是如何通过音频管理器更改手机模式?
答案 0 :(得分:8)
使用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;
}
答案 1 :(得分:2)
AudioManager manager = (AudioManager) getContext().getSystemService(AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
答案 2 :(得分:2)
试试这个:
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
答案 3 :(得分:0)
使其适用于Marshmallow及以上的Android设备(API 23)
在AndroidManifest.xml
中定义ACCESS_NOTIFICATION_POLICY
权限
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
在onResume/onCreate
函数中,如果未授权,请求获得许可
@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));
}
}
}
最后更改Ringer模式调用此方法
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);
}