Notification的实现之设置通知音乐.setSound 2017.06.06

xiaoxiao2021-02-28  129

今天在练习notification的时候设置后想到给通知触发的时候设置通知的音乐,由此引发了音乐音频文件资源的引用(Android资源文件的URI的引用)

Notification的实现很简单:步骤如下

1、实例化NotificationManger:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2、实例化一个通知:

Notification notification = new NotificationCompat.Builder(MainActivity.this) .setContentTitle("善水系统通知") .setContentText("欢迎注册善水系统") .setSmallIcon(R.mipmap.ic_launcher_round) .setWhen(System.currentTimeMillis()) //位图文件(Bitmap),setLargeIcon需要传入 位图文件,方式是BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) //设置跳转 .setContentIntent(pi) .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.rae)) .setAutoCancel(true) .build(); 3、显示Notification notificationManager.notify(1,notification);

上面的代码可以看到我们实现了Notification的通知 的设置,也实现了 点击Notification的触发时间,即PendingIntent;

另外实现了setSond设置通知音乐,在此我们的资源引用问题引出

setSound的参数是传入Uri,那怎样获取我们资源目录下的音频呢,我们可以通过以下:

如果直接访问Apk下的assets目录可以使用AssetManager类处理,但是我们访问res下的raw目录:

"android.resource://" + getPackageName() + "/" + R.raw.rae))

android.resource://

来引用内部资源,后面的getPackageName也可以是我们项目的包名,最后直接是我们的资源索引即可

最后说一下我们的Bitmap;Bitmap就是位图,位图是相对于矢量图,位图的引用我们使用的是:

BitmapFactory.decodeResource(getResource(),R.……),后面是图片索引

转载请注明原文地址: https://www.6miu.com/read-18365.html

最新回复(0)