歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 一個簡單手機響鈴功能實現

Android 一個簡單手機響鈴功能實現

日期:2017/3/1 10:31:20   编辑:Linux編程

當有新通知到達時,常常以響鈴方式提醒用戶。這裡主要講怎麼通過簡單的代碼來播放系統默認的鈴聲。請參閱下面的關鍵代碼:

[java]
  1. import java.util.Random;
  2. import Android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.Service;
  5. import android.content.Context;
  6. import android.media.AudioManager;
  7. public class TipHelper {
  8. // 播放默認鈴聲
  9. // 返回Notification id
  10. public static int PlaySound(final Context context) {
  11. NotificationManager mgr = (NotificationManager) context
  12. .getSystemService(Context.NOTIFICATION_SERVICE);
  13. Notification nt = new Notification();
  14. nt.defaults = Notification.DEFAULT_SOUND;
  15. int soundId = new Random(System.currentTimeMillis())
  16. .nextInt(Integer.MAX_VALUE);
  17. mgr.notify(soundId, nt);
  18. return soundId;
  19. }
  20. }
該方法的參數,傳遞Activity的引用即可。當然,上面的代碼,在靜音模式下,是無法播放的。
Copyright © Linux教程網 All Rights Reserved