作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
每当我使用它时,它都会使我的应用程序崩溃。此外,对于振动模式,当我使用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/
每当我使用它时,它都会使我的应用程序崩溃。此外,对于振动模式,当我使用audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE)时,它会打
当我执行这段代码时: mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT ); 顶部菜单栏中的图标设置为振动图标,振动设置设置为振动
我是一名优秀的程序员,十分优秀!