java - 弹出通知时如何播放声音?
问题描述
我正在开发一个应用程序,用户可以使用两个开关按钮打开/关闭通知和通知声音。我创建了在状态栏上弹出的通知,我想在它们出现时播放默认声音。我编写了以下代码,但它似乎不起作用。关于如何播放通知声音的任何想法?
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import java.util.Set;
import androidx.annotation.RequiresApi;
import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.example.myevents.R.drawable.notification;
public class Settings extends AppCompatActivity {
Switch simpleswitch1;
Switch simpleswitch2;
private Notification notification;
NotificationManager manager;
Notification myNotication;
boolean enableSound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
simpleswitch1.setChecked(false);
simpleswitch2.setChecked(false);
simpleswitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@TargetApi(Build.VERSION_CODES.P)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
int notifyID = 1;
String CHANNEL_SOUND_ID = "channel SOUND";// The id of the channel.
CharSequence NAME_SOUND = "channel 1";// The user-visible name of the channel.
String CHANNEL_SILENT_ID = "channel SILENT";// The id of the channel.
CharSequence NAME_SILENT = "channel 2";// The user-visible name of the channel.
int importance_sound = NotificationManager.IMPORTANCE_HIGH;
int importance_silent = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel_sound = new NotificationChannel(CHANNEL_SOUND_ID, NAME_SOUND, importance_sound);
NotificationChannel mChannel_silent = new NotificationChannel(CHANNEL_SILENT_ID, NAME_SILENT, importance_silent);
// Crete both notification channels
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel_sound);
mNotificationManager.createNotificationChannel(mChannel_silent);
Intent intent = new Intent(Settings.this, Visitor.class);
intent.putExtra("yourpackage.notifyId", notifyID);
PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Select the correct notification channel
String selectedChannelId;
if (enableSound) {
selectedChannelId = CHANNEL_SOUND_ID;
} else {
selectedChannelId = CHANNEL_SILENT_ID;
}
// Create a notification and set the notification channel.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Settings.this, selectedChannelId);
notificationBuilder.setSmallIcon(R.drawable.cheers);
notificationBuilder.setContentTitle("Title");
notificationBuilder.setContentText("Notification");
notificationBuilder.setContentIntent(pIntent);
notificationBuilder.setChannelId(selectedChannelId);
Notification notification = notificationBuilder.build();
// Issue the notification.
mNotificationManager.notify(notifyID, notification);
}}});
simpleswitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
enableSound = isChecked;
}
});
}}
解决方案
您应该定义一个标志来打开/关闭声音。这可以是由第二个开关控制的布尔值。然后在创建通知时检查该标志的状态并决定是否设置声音。
并且可能Notification
从 中分离NotificationBuilder
来控制声音。
1-创建作为类属性的标志
Switch simpleswitch1;
Switch simpleswitch2;
private Notification notification;
NotificationManager manager;
Notification myNotication;
// Sound disabled by default
boolean enableSound = false;
2-用第二个开关控制标志
simpleswitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
enableSound = isChecked;
}
});
3- 使用NotificationBuilder
来控制声音。从第一个开关中替换这段代码
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Settings.this, CHANNEL_ID);
notificationBuilder.setSmallIcon(R.drawable.notification);
notificationBuilder.setContentTitle("NOTIFICATION TITLE");
notificationBuilder.setContentText("You have a new notification");
notificationBuilder.setChannelId(CHANNEL_ID);
if (enableSound){
notificationBuilder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI);
notificationBuilder.setVibrate(new long[]{1000,100});
}
Notification notification = notificationBuilder.build();
我希望它有帮助!
更新
我想我知道为什么它没有播放声音。我刚刚创建了一个新的 Android Studio 项目并复制粘贴您的代码并触发通知,播放声音,但我无法关闭声音(只是相反)。而且我猜您的问题与通知渠道有关。然后我修改了我的应用程序,它工作了!
首先,让我纠正一下自己。自从api 级别 26(您正在使用根据PRIORITY
)以来已弃用,而您应该使用的是. 通知渠道的问题在于,您一旦创建就无法对其进行编辑(除非您卸载您的应用程序)。因此,如果您一开始使用低重要性通道,然后将其更改为高通道,则不会产生任何效果。@TargetApi(Build.VERSION_CODES.O)
IMPORTANCE
NotificationChannel
所以你真正需要的是两个通知通道:一个有声,一个无声,然后在之前创建的标志的帮助下选择合适的通道。
所以现在,第一个开关的代码是:注意我重新安排它,所以我首先创建 NotificationChannel(为了更好的可读性)
if (isChecked){
int notifyID = 1;
String CHANNEL_SOUND_ID = "channel SOUND";// The id of the channel.
CharSequence NAME_SOUND = "channel 1";// The user-visible name of the channel.
String CHANNEL_SILENT_ID = "channel SILENT";// The id of the channel.
CharSequence NAME_SILENT = "channel 2";// The user-visible name of the channel.
int importance_sound = NotificationManager.IMPORTANCE_HIGH;
int importance_silent = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel_sound = new NotificationChannel(CHANNEL_SOUND_ID, NAME_SOUND, importance_sound);
NotificationChannel mChannel_silent = new NotificationChannel(CHANNEL_SILENT_ID, NAME_SILENT, importance_silent);
// Crete both notification channels
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel_sound);
mNotificationManager.createNotificationChannel(mChannel_silent);
Intent intent = new Intent(Settings.this, Visitor.class);
intent.putExtra("yourpackage.notifyId", notifyID);
PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Select the correct notification channel
String selectedChannelId;
if (enableSound){
selectedChannelId = CHANNEL_SOUND_ID;
}else{
selectedChannelId = CHANNEL_SILENT_ID;
}
// Create a notification and set the notification channel.
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Settings.this, selectedChannelId);
notificationBuilder.setSmallIcon(R.drawable.notification);
notificationBuilder.setContentTitle("Title");
notificationBuilder.setContentText("Notification Text");
notificationBuilder.setContentIntent(pIntent);
notificationBuilder.setChannelId(selectedChannelId);
Notification notification = notificationBuilder.build();
// Issue the notification.
mNotificationManager.notify(notifyID , notification);
}
推荐阅读
- python - Django Money - 添加两个不同货币的 MoneyField 并以给定货币生成输出
- android - 为什么 Google Play 控制台的 Active Installs 一天内减少到 0?
- php - 使用php循环无法将数据库中的日期与当前日期匹配
- php - Laravel 路由问题。不工作 - 未定义错误
- sublimetext3 - Sublime thTheme 范围:回声中的变量
- vba - 用于批量延迟的涓流发送的 Outlook 宏?这可能吗?
- jekyll - "jekyll build --lsi" 使博客变得非常缓慢
- python - 有没有办法专门将 DNS 端口 53 绑定到 TCP 或 UDP?
- python - python中具有多输入多输出的并行函数调用
- oauth-2.0 - 验证 authlib 中的刷新令牌