Android O– 通知频道 – 更改振动模式或声音类型

Android O– 通知频道 – 更改振动模式或声音类型,第1张

概述使用AndroidO,我们可以获得“通知频道”.据我了解,这意味着用户无法再在APP内设置通知音或其他相关的通知设置.用户需要转到“通知通道设置”并更改音调或振动等,因为NotificationBuilder中的所有方法(如setSound)都会被忽略.所以真的没有办法通过代码将音调改为静音吗?或者通

使用Android O,我们可以获得“通知频道”.

据我了解,这意味着用户无法再在APP内设置通知音或其他相关的通知设置.

用户需要转到“通知通道设置”并更改音调或振动等,因为NotificationBuilder中的所有方法(如setSound)都会被忽略.

所以真的没有办法通过代码将音调改为静音吗?
或者通过代码改变振动模式?

例如,用户可以在我的应用程序中设置振动模式.
或者他可以从警报类型而不是通知类型中选择音调.

这一切都不可能了吗?
这是对的还是有办法做到这一点?

解决方法:

您仍然可以在应用中提供声音和振动自定义,但它需要不同的方法.简而言之,我们的想法是在AndroID O中手动播放声音和振动,而不是使用通知频道(它比看起来容易).

我就是这样做的:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelID);// builder.setSmallicon(...)// builder.setContentTitle(...)// builder.setContentText(...)if (androID.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {    // play vibration    vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);    vibrator.vibrate(VibrationEffect.createWaveform(vibrationPattern, -1));    // play sound    Intent serviceIntent = new Intent(context, SoundService.class);    serviceIntent.setAction("ACTION_START_PLAYBACK");    serviceIntent.putExtra("SOUND_URI", soundUri.toString());    context.startForegroundService(serviceIntent);    // the delete intent will stop the sound when the notification is cleared    Intent deleteIntent = new Intent(context, SoundService.class);    deleteIntent.setAction("ACTION_Stop_PLAYBACK");    PendingIntent pendingDeleteIntent =            PendingIntent.getService(context, 0, deleteIntent, 0);    builder.setDeleteIntent(pendingDeleteIntent);} else {    builder.setVibrate(vibrationPattern);    builder.setSound(soundUri);}notificationmanager.notify(notificationID, builder.build());

SoundService.class是我用MediaPlayer播放声音的地方:

public class SoundService extends Service {    MediaPlayer mMediaPlayer;    @OverrIDe    public IBinder onBind(Intent intent) {        return null;    }    public int onStartCommand(Intent intent, int flags, int startID) {        // foreground notification        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            NotificationCompat.Builder builder =                new NotificationCompat.Builder(this, otherChannelID);            builder.setSmallicon(...)                    .setContentTitle(...)                    .setContentText(...)                    .setautoCancel(true);            startForeground(foregroundNotificationID, builder.build());        }        // check action        String action = intent.getAction();        switch (action) {            case "ACTION_START_PLAYBACK":                startSound(intent.getStringExtra("SOUND_URI"));                break;            case "ACTION_Stop_PLAYBACK":                stopSound();                break;        }        // service will not be recreated if abnormally terminated        return START_NOT_STICKY;    }    private voID startSound(String uriString) {        // parse sound        Uri soundUri;        try {            soundUri = Uri.parse(uriString);        } catch (Exception e) {            cleanup();            return;        }        // play sound        if (mMediaPlayer == null) {            mMediaPlayer = new MediaPlayer();            mMediaPlayer.setonPreparedListener(new MediaPlayer.OnPreparedListener() {                @OverrIDe                public voID onPrepared(MediaPlayer mp) {                    mp.start();                }            });            mMediaPlayer.setonCompletionListener(new MediaPlayer.OnCompletionListener() {                @OverrIDe                public voID onCompletion(MediaPlayer mediaPlayer) {                    cleanup();                }            });        }        try {            mMediaPlayer.setDataSource(this, soundUri);            mMediaPlayer.prepareAsync();        } catch (Exception e) {            cleanup();        }    }    private voID stopSound() {        if (mMediaPlayer != null) {            mMediaPlayer.stop();            mMediaPlayer.release();            mMediaPlayer = null;        }        cleanup();    }    private voID cleanup() {        stopSelf();    }}

建议

>使用importANCE_DEFAulT(对于用户,这是’高’),空声音(setSound(null,null))和空振动(setVibrationPattern(null))创建通知通道,并在通道描述中解释这是推荐的设置以避免与应用程序自己的自定义冲突.
>将整个事情变成您的青睐:不要删除功能,而是为用户提供新功能.您可以让他们有机会使用您的自定义功能或通知渠道功能(例如,您可以检查当前渠道的重要性,并根据您可以使用的一个或另一个级别).

前景通知

启动AndroID O后,必须从后台启动的服务作为前台服务启动.这意味着SoundService需要前台通知.你有一些选择:

>使用“停止播放”按钮创建一个漂亮的前景通知,以便用户可以在不删除启动声音的情况下停止声音.
>创建一个简单的通知并将其发送到禁用的通道(如果使用importANCE_NONE创建它们,则可以创建禁用的通道).执行此 *** 作时,将显示默认系统“应用程序在后台运行”通知而不是前台通知,但用户可以根据需要隐藏状态栏中的此通知.

编辑:在AndroID 8.1中,使用importANCE_NONE创建禁用频道似乎没有用,因为在发送通知时将自动启用该频道.最好从一开始就使用importANCE_LOW创建它,并让用户根据需要更改重要性.

总结

以上是内存溢出为你收集整理的Android O – 通知频道 – 更改振动模式或声音类型全部内容,希望文章能够帮你解决Android O – 通知频道 – 更改振动模式或声音类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1100607.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存