7

我正在制作应用程序,如果用户发送带有特定关键字的短信,电话模式将从静音模式变为振铃模式。该应用程序主要在用户在家中某处丢失手机并处于静音模式时工作。我的主要问题是如何通过音频管理器更改电话模式?

4

4 回答 4

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;   
}  
于 2012-06-19T10:41:54.407 回答
2
AudioManager manager = (AudioManager) getContext().getSystemService(AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
于 2012-06-19T10:39:46.313 回答
2

尝试这个:

AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
于 2012-06-19T10:40:12.787 回答
0

使其适用于棉花糖及以上的安卓设备(API 23)

  1. AndroidManifest.xml定义ACCESS_NOTIFICATION_POLICY权限

    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
    
  2. 如果未授予,请在您的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));
            }
        }
    }
    
  3. 最后改变铃声模式调用这个方法

    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);
        }
    
于 2017-09-16T06:22:19.370 回答