天天看點

Android 擷取手機系統的聲音設定管理通知提醒的聲音

通知提醒對很多需要推送的應用來說是必不可少的,但是有的推送需要聲音或者震動,也有的因為開會的話想要一個靜音的,那麼我們應該如何設定的,于是我就研究了一下,首先我們就要擷取到手機系統聲音的目前設定,代碼如下

AudioManager am = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        final int ringerMode = am.getRingerMode();
           

ringerMode為手機的系統聲音設定的狀态值,0位靜音,1為震動,2為響鈴,下面就是通知的設定

mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotification = new Notification();
        Intent intent = new Intent(context, SplashActivity.class);
        intent.putExtra("notification_main_flag", );
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent mPendingIntent = PendingIntent.getActivity(context, ,
                intent, );
        mNotification.icon = R.drawable.ic_launcher1;
        mNotification.contentIntent = mPendingIntent;
        CharSequence contentTitle = getTime();
        mNotification.setLatestEventInfo(context, contentTitle, data,
                mPendingIntent);
        AudioManager am = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        final int ringerMode = am.getRingerMode();
        if (ringerMode == MUTE) {
            //
        }
        if (ringerMode == VIBRATE) {
            mNotification.defaults |= Notification.DEFAULT_VIBRATE;// 震動
            long v1[] = { , , ,  }; // 震動頻率
            mNotification.vibrate = v1;
        }
        if (ringerMode == SOUND) {
            mNotification.defaults |= Notification.DEFAULT_SOUND;// 聲音
        }

        mNotification.flags |= Notification.FLAG_AUTO_CANCEL;// 點選消息後,該消息自動退出
        mNotificationManager.cancel(NOTIFICATION_ID);
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
           

如果還有什麼不懂得地方,歡迎留言,或者加Android技術交流群 50208422或Android交流群 470707794為你解決