我正在尝试创建一个将振铃器模式设置为从振动或正常模式静音的应用程序,我检查了很多时间的代码,但无法在代码中找到问题。希望社区能够找到问题。并帮助我解决。
public class MainActivity extends AppCompatActivity {
private AudioManager myAudioManager;
private Button silentBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
silentBtn = findViewById(R.id.button);
myAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
silentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mod = myAudioManager.getRingerMode();
if (mod == AudioManager.RINGER_MODE_SILENT) {
Toast toast = Toast.makeText(MainActivity.this, "You have wasted your time by clicking this Button.", Toast.LENGTH_SHORT);
toast.show();
} else {
myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast toast1 = Toast.makeText(MainActivity.this, "You are Good to Go.", Toast.LENGTH_SHORT);
toast1.show();
}
}
});
}
}
我希望单击该按钮后该应用程序不会崩溃。
答案 0 :(得分:0)
尝试在manifest.xml中添加此权限
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"