1. 程式人生 > >android 自定義notification的提示音

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);
	}