Android Notification 詳解,MediaPlayer 一直播放系統鈴聲
背景知識</span>
- 要使用Android通知必須使用到Android通知管理器:NotificationManager管理這個應用程序的通知,每個notify都有唯一標識符即ID。用于管理更新這個通知內容……
- 當然還是需要添加相應的權限滴!比如響鈴,震動…… </ul>
代碼解析
1.創建通知管理器
NotificationManager 是一個系統Service,必須通過 getSystemService()方法來獲取。
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
2.創建通知</span></span> </h3>
Notification:是可以設置icon、文字、提示聲音、振動等等參數。</span>
int icon = R.drawable.wlicon;
long when = System.currentTimeMillis();//時間
// 創建通知欄的顯示
CharSequence tickerText = "未讀消息提醒";
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_LIGHTS; // 通知燈光
notification.defaults |= Notification.DEFAULT_VIBRATE; // 震動
notification.flags |= Notification.FLAG_NO_CLEAR; // 通知不可以清除
// notification.flags = Notification.FLAG_AUTO_CANCEL; // 通知可以清除
// notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // 系統默認鈴聲
// notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");// 播放自定義的鈴聲
// notification.flags |= Notification.FLAG_INSISTENT; // 聲音一直響到用戶相應,就是通知會一直響起,直到你觸碰通知欄的時間就會停止
// 創建后在狀態欄中通知的內容
Context context = droidGap.getApplicationContext();
CharSequence contentTitle = "未讀消息提醒";
CharSequence contentText = "您有" + Quantity + "條未讀消息,請及時讀取。";
// 點擊后打開的項目 創建一個Intent
Intent notificationIntent = new Intent(droidGap, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(droidGap, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
完整例子
這里沒有使用通知欄的響鈴是因為用戶下拉通知欄的時間那個響鈴會停止,我需求是需要一直播放。所以使用了MediaPlayer
/**
* 創建通知欄
* @param Quantity 數量
* @param player MediaPlayer 播放聲音
*/
public void createNotification(String Quantity, MediaPlayer player) {
// 創建通知欄
NotificationManager notificationManager = (NotificationManager) droidGap.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.wlicon;
long when = System.currentTimeMillis();
// 創建通知欄的顯示
CharSequence tickerText = "未讀消息提醒";
Notification notification = new Notification(icon, tickerText, when);
notification.defaults |= Notification.DEFAULT_LIGHTS; // 通知燈光
notification.defaults |= Notification.DEFAULT_VIBRATE; // 震動
notification.flags |= Notification.FLAG_NO_CLEAR; // 通知不可以清除
// notification.flags = Notification.FLAG_AUTO_CANCEL; // 通知可以清除
// notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // 系統默認鈴聲
// notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");// 播放自定義的鈴聲
// notification.flags |= Notification.FLAG_INSISTENT; // 聲音一直響到用戶相應,就是通知會一直響起,直到你觸碰通知欄的時間就會停止
// 創建后在狀態欄中通知的內容
Context context = droidGap.getApplicationContext();
CharSequence contentTitle = "未讀消息提醒";
CharSequence contentText = "您有" + Quantity + "條未讀消息,請及時讀取。";
// 點擊后打開的項目 創建一個Intent
Intent notificationIntent = new Intent(droidGap, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(droidGap, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
try {
if (Integer.parseInt(Quantity) == 0 && player.isPlaying()) {
player.reset(); // 到初始化狀態,這里需要判斷是否正在響鈴,如果直接在開啟一次會出現2個鈴聲一直循環響起,您不信可以嘗試
} else if (!player.isPlaying()) {
NotificationUtil.ring(player);
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
/**
* 一直響鈴
* @param droidGap
* @param player
* @return
* @throws Exception
* @throws IOException
*/
private static MediaPlayer ring(MediaPlayer player) throws Exception, IOException {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// MediaPlayer player = new MediaPlayer();
player.setDataSource(droidGap, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {
player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
player.setLooping(true);
player.prepare();
player.start();
}
return player;
}</pre>
</span>
* 創建通知欄
* @param Quantity 數量
* @param player MediaPlayer 播放聲音
*/
public void createNotification(String Quantity, MediaPlayer player) {
// 創建通知欄 NotificationManager notificationManager = (NotificationManager) droidGap.getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.wlicon; long when = System.currentTimeMillis(); // 創建通知欄的顯示 CharSequence tickerText = "未讀消息提醒"; Notification notification = new Notification(icon, tickerText, when); notification.defaults |= Notification.DEFAULT_LIGHTS; // 通知燈光 notification.defaults |= Notification.DEFAULT_VIBRATE; // 震動 notification.flags |= Notification.FLAG_NO_CLEAR; // 通知不可以清除 // notification.flags = Notification.FLAG_AUTO_CANCEL; // 通知可以清除 // notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // 系統默認鈴聲 // notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");// 播放自定義的鈴聲 // notification.flags |= Notification.FLAG_INSISTENT; // 聲音一直響到用戶相應,就是通知會一直響起,直到你觸碰通知欄的時間就會停止 // 創建后在狀態欄中通知的內容 Context context = droidGap.getApplicationContext(); CharSequence contentTitle = "未讀消息提醒"; CharSequence contentText = "您有" + Quantity + "條未讀消息,請及時讀取。"; // 點擊后打開的項目 創建一個Intent Intent notificationIntent = new Intent(droidGap, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(droidGap, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); notificationManager.notify(NOTIFICATION_ID, notification); try { if (Integer.parseInt(Quantity) == 0 && player.isPlaying()) { player.reset(); // 到初始化狀態,這里需要判斷是否正在響鈴,如果直接在開啟一次會出現2個鈴聲一直循環響起,您不信可以嘗試 } else if (!player.isPlaying()) { NotificationUtil.ring(player); } } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }
/**
* 一直響鈴
* @param droidGap
* @param player
* @return
* @throws Exception
* @throws IOException
*/
private static MediaPlayer ring(MediaPlayer player) throws Exception, IOException {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// MediaPlayer player = new MediaPlayer();
player.setDataSource(droidGap, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {
player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
player.setLooping(true);
player.prepare();
player.start();
}
return player;
}</pre></span>