如何在Android中添加自定义通知声音

如何在Android中添加自定义通知声音

本文介绍了如何在Android中添加自定义通知声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须管理通知,为此我需要管理自定义声音。所以,你有什么想法,我们可以做到这一点?



我已经将声音文件复制到我的原始文件夹,所以任何人都有任何想法我可以如何实现它到我的项目。



预先感谢。 解决方案

Resource (res)将其命名为 raw ,并将文件(YOUR_SOUND_FILE.MP3)放入其中,并使用下面的代码行定制声音

  NotificationManager notificationManager =(NotificationManager)context 
.getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新通知(icon,message,when);

String title = context.getString(R.string.app_name);

Intent notificationIntent = new Intent(context,
SlidingMenuActivity.class);
notificationIntent.putExtra(isInbox,true);
//设置意图,因此它不会启动新的活动
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context,0,
notificationIntent,0);
notification.setLatestEventInfo(context,title,message,intent);
notification.flags | = Notification.FLAG_AUTO_CANCEL;

将这些代码行用于自定义声音

  notification.sound = Uri.parse(android.resource://+ context.getPackageName()+/+ R.raw.FILE_NAME ); //这里是FILE_NAME是你想要播放的文件的名称


//振动如果振动被启用
notification.defaults | = Notification.DEFAULT_VIBRATE;
notificationManager.notify(0,notification);


I have to manage notification and for that I need to manage custom sound when it comes. So do you have any idea how we can do this?

I already copied sound file to my raw folder, so anyone have any ideas how I can implement it to my project.

Thanks in advance.

解决方案

Firstly make the folder in Resource (res) name it raw and put the file (YOUR_SOUND_FILE.MP3) in it and than use below lines of code for custom sound

   NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context,
            SlidingMenuActivity.class);
    notificationIntent.putExtra("isInbox", true);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

Use these lines of code for custom sound

 notification.sound =Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.FILE_NAME);//Here is FILE_NAME is the name of file that you want to play


    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

这篇关于如何在Android中添加自定义通知声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:57