NotificationManager的用法

代碼解析oop


1.建立通知管理器.net

NotificationManager 是一個系統Service,必須經過 getSystemService()方法來獲取。get


NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);直播

2.建立通知it

Notification:是能夠設置icon、文字、提示聲音、振動等等參數。 io


int icon = R.drawable.wlicon;class

        long when = System.currentTimeMillis();//時間test

//      建立通知欄的顯示file

        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;

    }

相關文章
相關標籤/搜索