android - 如何以正确的方式android设置通知声音持续时间

标签 android notifications android-notifications

我是安卓新手。我正在尝试创建具有自定义声音持续时间的通知。我见过很多具有此功能的应用程序,但我不明白如何以最有用的方式制作它。

对于我的通知,我使用 BroadcastReceiver:

public class NotificationAlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {

setNotification(context);

}

 private void setNotification(Context context) {
    Intent intent = new Intent(context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP |
            Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,
            (int) System.currentTimeMillis(), intent, 0);
    Notification notification = new Notification.Builder(context).
            setTicker("Hi!").
            setAutoCancel(true).
            setContentTitle("Bla bla lba!").
            setContentText("Hi hi!").
            setSmallIcon(R.mipmap.ic_launcher).
            setContentIntent(pendingIntent).build();

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;

    notification.flags |= Notification.FLAG_INSISTENT;

    notificationManager.notify((int) System.currentTimeMillis(), notification);
}
}

谢谢!

最佳答案

我没有找到任何方法来控制声音持续时间,但您可以使用以下代码将 AudioManager.STREAM_NOTIFICATION 静音:-

final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_NOTIFICATION, true or false);

不要忘记在通知声音总持续时间结束后取消静音。

关于android - 如何以正确的方式android设置通知声音持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489317/

相关文章:

android - 如何在android通知中增加remoteview的高度

android - 收到通知时出现异常

android - TabHost 中的 Horizo​​ntalScrollView 在末尾添加额外的空间

java - 从 imageview Android 中删除图像

iOS - 向远程通知添加操作。我错过了什么?

flutter - 如何解决 Flutter 中的 Android PlatformException INVALID_ICON 错误?

javascript - Android Phonegap 3 webapp 白名单不起作用

android - getWritableDatabase() -Nullpoiter 异常 (android)

python - 每当我的 MySQL 数据库发生变化(插入)时,是否可以向我的 python 应用程序发送实时通知?

android - 创建NotificationChannel会抛出IllegalArgumentException