java - audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT) 不起作用

标签 java android audio vibration

每当我使用它时,它都会使我的应用程序崩溃。 此外,对于振动模式,当我使用audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE)时,它会打开“请勿打扰”,并且不会将铃声模式更改为振动。

这是主要 Activity 文件。

package com.example.soundcontroller;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    private static final String TAG = "Hani" ;
    Button ring,vibrate,mute;
    AudioManager audioManager;
  //  private NotificationManager mNotificationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

        Log.d(TAG, "onCreate: Start");
        ring = (Button) findViewById(R.id.btnRing);
        vibrate = (Button) findViewById(R.id.btnVibrate);
        mute = (Button) findViewById(R.id.btnSilent);
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int currentMode = audioManager.getRingerMode();
        ring.setBackgroundResource(R.color.colorPrimaryDark);
        mute.setBackgroundResource(R.color.colorPrimaryDark);
        vibrate.setBackgroundResource(R.color.colorPrimaryDark);
       // mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Log.d(TAG, "onCreate: everything set");
      if (currentMode == audioManager.RINGER_MODE_NORMAL) {

            ring.setBackgroundResource(R.color.colorAccent);
            Log.d(TAG, "onCreate: color set ringer");
        } else if (currentMode == audioManager.RINGER_MODE_SILENT) {

            mute.setBackgroundResource(R.color.colorAccent);
            Log.d(TAG, "onCreate: color set mute");
        } else if (currentMode == audioManager.RINGER_MODE_VIBRATE) {

            vibrate.setBackgroundResource(R.color.colorAccent);
            Log.d(TAG, "onCreate: color set vibrate");
        }
        ring.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d(TAG, "onCreate: clicked on ring");
                try {
                    audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                }
                catch(Exception e) {
                    Toast.makeText(getApplicationContext(), "Something Went Wrong", Toast.LENGTH_SHORT).show();

                }





                Log.d(TAG, "onCreate: ringer mode set to normal");
                ring.setBackgroundResource(R.color.colorAccent);
                vibrate.setBackgroundResource(R.color.colorPrimaryDark);
                mute.setBackgroundResource(R.color.colorPrimaryDark);
                Toast.makeText(getApplicationContext(), "Ring mode Activated", Toast.LENGTH_SHORT).show();

            }
        });

        mute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onCreate: clicked on mute");

                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                mute.setBackgroundResource(R.color.colorAccent);
                vibrate.setBackgroundResource(R.color.colorPrimaryDark);
                ring.setBackgroundResource(R.color.colorPrimaryDark);
                Toast.makeText(getApplicationContext(), "Silent mode Activated", Toast.LENGTH_SHORT).show();

            }
        });

        vibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onCreate: clicked on vibrate");
                //if(mNotificationManager.getCurrentInterruptionFilter()==mNotificationManager.INTERRUPTION_FILTER_NONE)
               //     mNotificationManager.setInterruptionFilter(INTERRUPTION_FILTER_ALL);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                vibrate.setBackgroundResource(R.color.colorAccent);
                ring.setBackgroundResource(R.color.colorPrimaryDark);
                mute.setBackgroundResource(R.color.colorPrimaryDark);
                Toast.makeText(getApplicationContext(), "Vibration mode Activated", Toast.LENGTH_SHORT).show();
            }
        });


    }

}


list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.soundcontroller">

    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>



</manifest>

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<Button
android:id="@+id/btnRing"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="150dp"
android:background="@color/colorPrimary"
android:layout_marginBottom="5dp"
android:text="Ring Mode"
android:textColor="@color/colorWhite" />
<Button
android:id="@+id/btnSilent"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
  android:layout_below="@+id/btnRing"
android:background="@color/colorPrimary"
android:layout_marginBottom="5dp"
android:text="Silent Mode"
android:textColor="@color/colorWhite" />
<Button
android:id="@+id/btnVibrate"
android:layout_width="150dp"
android:layout_height="wrap_content"
    android:layout_below="@+id/btnSilent"
android:layout_marginLeft="130dp"
android:background="@color/colorPrimary"
android:text="Vibrate Mode"
android:textColor="@color/colorWhite" />
    </RelativeLayout>

最佳答案

当我手动向我的应用程序授予“请勿打扰”权限时,它开始完全正常工作。

关于java - audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62182476/

相关文章:

java - 如何使用 JFileChooser 将 CSV 文件数据输入到 JTable 中

java - 未找到使用 img 标签在 servlet 中显示的图像

java - 使用 Stream API 获取具有匹配键的映射条目

java - 无法在 Android 项目中使用 Eclipse 的剪贴簿

java - 如何修复: error: not an enclosing class: Context

java - 比较不同规范的mp3和wav文件的最佳方法?

android - 如何唤醒另一个Android应用程序?

Android::Service OnDestroy 调用得很晚

iOS - 同时发送和接收数据

iphone - iPhone SDK:AVAudioPlayer在暂停或结束时发出声音