【发布时间】:2020-10-08 04:22:54
【问题描述】:
我正在构建一个 Android 应用,并尝试在应用启动时禁用设备的所有声音和振动。
我是新手,所以我不知道该怎么做。
有什么想法吗?
提前致谢。
【问题讨论】:
标签: android
我正在构建一个 Android 应用,并尝试在应用启动时禁用设备的所有声音和振动。
我是新手,所以我不知道该怎么做。
有什么想法吗?
提前致谢。
【问题讨论】:
标签: android
谢谢! :) 我自己回复以完成答案:
AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
aManager.setRingerMode(aManager.RINGER_MODE_SILENT);
【讨论】:
F 不是元音 BTW ;)
在此处查看this,并使用RINGER_MODE_SILENT 选项查看public void setRingerMode (int ringerMode)。
【讨论】:
您需要先在清单文件中添加更改音频设置的权限。为了能够将振铃模式设置为静音,您必须请求访问通知策略的权限
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
然后在onClick 事件中(下面是textView),您可以一一切换声音设置:
// attach an OnClickListener
audioToggle.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// your click actions go here
NotificationManager notificationManager = (NotificationManager) getActivity().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
return;
}
ToggleDoNotDisturb(notificationManager);
}
});
private void ToggleDoNotDisturb(NotificationManager notificationManager) {
if (notificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALL) {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
audioToggle.setText(R.string.fa_volume_mute);
} else {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
audioToggle.setText(R.string.fa_volume_up);
}
}
你还需要检查权限
NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
【讨论】: