android 自定义notification的提示音

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 {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification);

		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				addNotificaction();

			}
		});

	}

	/**
	 * 添加一个notification
	 */
	private void addNotificaction() {
		NotificationManager manager = (NotificationManager) this
				.getSystemService(Context.NOTIFICATION_SERVICE);
		// 创建一个Notification
		Notification 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);

	@Override
	protected 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);
	}


  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值