android 自定义notification的提示音

来源:互联网 发布:cad批量打印软件 编辑:程序博客网 时间:2024/06/06 03:29
package com.example.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore.Audio;import android.view.View;import android.widget.Button;/*** * @description 状态栏通知相关 * @author chenzheng_java *  */public class NotificationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.notification);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addNotificaction();}});}/** * 添加一个notification */private void addNotificaction() {NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);// 创建一个NotificationNotification notification = new Notification();// 设置显示在手机最上边的状态栏的图标notification.icon = R.drawable.ic_launcher;// 当当前的notification被放到状态栏上的时候,提示内容notification.tickerText = "注意了,我被扔到状态栏了";/*** * notification.contentIntent:一个PendingIntent对象,当用户点击了状态栏上的图标时, * 该Intent会被触发 notification.contentView:我们可以不在状态栏放图标而是放一个view * notification.deleteIntent 当当前notification被移除时执行的intent * notification.vibrate 当手机震动时,震动周期设置 */// 添加声音提示notification.defaults |= Notification.DEFAULT_SOUND;// audioStreamType的值必须AudioManager中的值,代表着响铃的模式notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;// 下边的两个方式可以添加音乐// notification.sound = Uri.parse("file:///sdcard/notifications/hot_like_small.ogg");//notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");//notification.defaults = Notification.DEFAULT_LIGHTS;//notification.sound = Uri.parse("content://media/internal/audio/media/80");notification.defaults = Notification.DEFAULT_LIGHTS;          notification.sound = Uri.parse("android.resource://" + getPackageName()                  + "/" + R.raw.sound_pull_down);Intent intent = new Intent(this, Notification2Activity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_ONE_SHOT);// 点击状态栏的图标出现的提示信息设置notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件",pendingIntent);manager.notify(1, notification);}}


铃声的来源可以是内置在应用程序内部raw目录下的资源文件,也可以通过打开系统铃声选择页面去选择:

1、内置在应用程序内部raw目录下的资源文件

notification.sound = Uri.parse("android.resource://" + getPackageName()                  + "/" + R.raw.sound_pull_down);

2、通过打开系统铃声选择页面去选择

Intent i = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);i.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);i.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);i.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);Uri ringtongUri = null;String uri = SharedPreferenceUtil.getInfoFromShared(SharedPreferenceUtil.KEY_NOTIFY_SOUND_URI);if (!ResourceUtils.isEmpty(uri)) {ringtongUri = android.net.Uri.parse(uri);} else {ringtongUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);}i.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.notification_ringtong));i.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtongUri);startActivityForResult(i, REQUEST_NOTIFY_SOUND);

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == REQUEST_NOTIFY_SOUND) {if (resultCode == RESULT_OK) {Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);// RingtoneManager.isDefault(uri);if (uri != null) {String ringTonePath = uri.toString();SharedPreferenceUtil.setInfoToShared(SharedPreferenceUtil.KEY_NOTIFY_SOUND_URI, ringTonePath);} else {SharedPreferenceUtil.setInfoToShared(SharedPreferenceUtil.KEY_NOTIFY_SOUND_URI, "");}}}super.onActivityResult(requestCode, resultCode, data);}


0 0
原创粉丝点击