java 显示声音提示,通知弹出时如何播放声音?

I'm working on an app where using two switch buttons the user can turn on/off notifications and notification sound. I created the notifications that pop up on the status bar and I want to play a default sound when they appear.I wrote the following code but it doesn't seem to work. Any ideas about how can I make the notification sound play ?

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;

}

});

}}

解决方案

You should define a flag to toggle the sound on/off. This could be a boolean value controlled by the second switch. Then check the status of that flag when creating the notification and decide to set or not the sound.

And maybe split the Notification from the NotificationBuilder to control the sound.

1- Create the flag being a Class attribute

Switch simpleswitch1;

Switch simpleswitch2;

private Notification notification;

NotificationManager manager;

Notification myNotication;

// Sound disabled by default

boolean enableSound = false;

2- Control the flag with the second switch

simpleswitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

enableSound = isChecked;

}

});

3- Use the NotificationBuilder to control the sound. Replace this piece of code from first switch

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();

I hope it helps!

UPDATE

I think I know why it's not playing a sound. I've just created a new Android Studio Project and copy-paste your code and the notifications is fired, the sound plays and I can not turn the sound off (just the oppostite). And I'm guessing your problem is related to the notification channel. Then I modify my App and it worked!

First, let me correct myself. The PRIORITY has been deprecated since api level 26 (which you are using according to @TargetApi(Build.VERSION_CODES.O)) and what you should use instead is the IMPORTANCE in the NotificationChannel. The problem with a notification channel is that you can not edit it once created (unless you uninstall your app). So if at first you where using a low importance channel and then change it to a high one it won't take any effect.

So what you really need is two notifications channel: one with sound and one silent, and then select the appropiate channel with the help of the previously created flag.

So now, the code for the first switch would be: Notice I re-arrange it so I first create the NotificationChannel (for better readability)

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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值