每当我使用它时,它就会崩溃我的应用程序。
此外,对于振动模式,当我使用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();
}
});
}
}
表现
<?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>
最佳答案
当我手动向我的应用程序授予“请勿打扰”许可时,它开始完全正常运行。