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

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

[java] import java.util.Random; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.Service; 
import android.content.Context; 
import android.media.AudioManager; 
 
public class TipHelper { 
 
    // 播放默認鈴聲  
    // 返回Notification id  
    public static int PlaySound(final Context context) { 
        NotificationManager mgr = (NotificationManager) context 
                .getSystemService(Context.NOTIFICATION_SERVICE); 
        Notification nt = new Notification(); 
        nt.defaults = Notification.DEFAULT_SOUND; 
        int soundId = new Random(System.currentTimeMillis()) 
                .nextInt(Integer.MAX_VALUE); 
        mgr.notify(soundId, nt); 
        return soundId; 
    } 

import java.util.Random;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.media.AudioManager;

public class TipHelper {

 // 播放默認鈴聲
 // 返回Notification id
 public static int PlaySound(final Context context) {
  NotificationManager mgr = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
  Notification nt = new Notification();
  nt.defaults = Notification.DEFAULT_SOUND;
  int soundId = new Random(System.currentTimeMillis())
    .nextInt(Integer.MAX_VALUE);
  mgr.notify(soundId, nt);
  return soundId;
 }
}

            該方法的參數,傳遞Activity的引用即可。當然,上面的代碼,在靜音模式下,是無法播放的。

摘自 心靈凈土

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *