簡述:
自己遇到的需求:在自己app?中余佃,全局監(jiān)聽音量變化暮刃,就是用戶按設(shè)備的加減聲音鍵,然后將音量變化展示到界面上(一個seekbar)爆土。
正文:
1:監(jiān)聽系統(tǒng)音量變化(廣播)
因為設(shè)備是安卓11的椭懊,記得好像安卓版本高于8.0?不允許靜態(tài)廣播監(jiān)聽系統(tǒng)廣播,沒辦法步势,只能動態(tài)注冊氧猬。上代碼吧:
廣播類:
public class MediaButtonIntentRecieverextends BroadcastReceiver{
? ? private static final StringTAG = MediaButtonIntentReciever.class.getSimpleName();
? ? private AudioManagermAudioManager;
? ? private PopupWindowmPopupWindow;
? ? private int mMediaMaxVolume;
? ? private int mMediaVolume;
? ? private SeekBarmCar_volume;
? ? private CountDownTimercountDownTimer;
? ? public MediaButtonIntentReciever() {
}
? ? @Override
? ? public void onReceive(Context context, Intent intent) {
? ? ? ? if (intent !=null) {
? ? ? ? ? ? if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
? ? ? ? ? ? ? ? //監(jiān)聽音量
? ? ? ? ? ? ? ? InitVolume(context);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //監(jiān)聽音量
? ? private void InitVolume(Context context) {
? ? ? ? //獲取音量管理器
? ? ? ? if (mAudioManager ==null) {
? ? ? ? ? ? mAudioManager =(AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
? ? ? ? }
? ? ? ? //當(dāng)前音量
? ? ? ? mMediaVolume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
? ? ? ? //最大音量
? ? ? ? mMediaMaxVolume =mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
? ? ? ? LogUtil.e("===當(dāng)前音量:" +mMediaVolume);
? ? ? ? startPopupWindow(context);
? ? }
? ? private void startPopupWindow(Context context) {
? ? ? ? View inflate = LayoutInflater.from(context).inflate(R.layout.volume_popup, null);
? ? ? ? if (mPopupWindow ==null) {
? ? ? ? ? ? mPopupWindow =new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
? ? ? ? }
? ? ? ? mPopupWindow.setBackgroundDrawable(null);
? ? ? ? mPopupWindow.setOutsideTouchable(true);
? ? ? ? //主界面linealayout的id,并給自己定義的popwindow xml文件backgrount? ? android:background="#33000000"
? ? ? ? mPopupWindow.showAtLocation(new View(context), Gravity.TOP, Gravity.CENTER_HORIZONTAL, Gravity.TOP);
? ? ? ? init();
? ? ? ? inflate.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? mPopupWindow.dismiss();
? ? ? ? ? ? ? ? countDownTimer.cancel();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? if (mCar_volume ==null) {
? ? ? ? ? ? mCar_volume = inflate.findViewById(R.id.car_volume);
? ? ? ? }
? ? ? ? //seekbar設(shè)置最大值為最大音量
? ? ? ? mCar_volume.setMax(mMediaMaxVolume);
? ? ? ? mCar_volume.setOnTouchListener(new View.OnTouchListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onTouch(View v, MotionEvent event) {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? mCar_volume.setProgress(mMediaVolume);
? ? }
? ? //seekbar設(shè)置當(dāng)前進度為當(dāng)前音量
? ? private void setView() {
? ? ? ? LogUtil.e("===音量改變:" +mMediaVolume);
? ? ? ? mCar_volume.setProgress(mMediaVolume);
? ? }
? ? private void init() {
? ? ? ? //重新計時
? ? ? ? if (countDownTimer !=null) {
? ? ? ? ? ? countDownTimer.cancel();
? ? ? ? }
? ? ? ? //初始化CountTimer坏瘩,設(shè)置倒計時時間
? ? ? ? countDownTimer =new CountDownTimer(5000, 1000) {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onTick(long l) {
//? ? ? ? ? ? ? ? onActivityTick(l);
? ? ? ? ? ? ? ? LogUtil.e("音量彈窗倒計時--------" + l);
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFinish() {
? ? ? ? ? ? ? ? //長時間未操作 啟動廣告頁
//? ? ? ? ? ? ? ? onActivityFinish();
? ? ? ? ? ? ? ? mPopupWindow.dismiss();
? ? ? ? ? ? ? ? countDownTimer.cancel();
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? new Handler(getMainLooper()).post(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? countDownTimer.start();
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void timeStart() {
}
}
broadcastreceiver中代碼有點亂盅抚,懶得改了,著急總結(jié)完下班 -.- !?有注釋倔矾,相信差不多都能看懂
可以在onReceive回調(diào)中妄均,進行處理自己想要的。我這邊是彈出一個popupwindow,里面有一個seekbar丛晦,根據(jù)音量調(diào)節(jié)奕纫,seekbar進行滑動。
還有一個五秒消失seekbar烫沙,當(dāng)音量發(fā)生改變匹层,倒計時重新計時。
? ? 清單文件:
? ??<receiver
????android:name=".receiver.MediaButtonIntentReciever"
? ? android:permission="android.permission.RECEIVE_VOLUME_CHANGED_ACTION"
? ? android:enabled="true"
? ? android:exported="true">
? ? <intent-filter>
? ? ? ? <action android:name="android.media.VOLUME_CHANGED_ACTION" />
? ? </intent-filter>
</receiver>
首先在清單文件注冊锌蓄。
因為是全局監(jiān)聽的升筏,所以我這邊是直接在base層的activity中,進行注冊廣播瘸爽。
下面分別是注冊廣播代碼和銷毀廣播代碼:
private void initBroatCastReceiver() {
? ? mIntentFilter =new IntentFilter();
? ? mIntentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
? ? mReciever =new MediaButtonIntentReciever();
? ? registerReceiver(mReciever, mIntentFilter);
? ? LogUtil.e("=====注冊廣播");
}
private void onDestoryRevicer() {
? ? unregisterReceiver(mReciever);
? ? LogUtil.e("=====銷毀廣播");
}
因為是全局的您访,所以寫在了baseActivity中,之后在baseActivity中的onStart和onPase回調(diào)中分別調(diào)用注冊和注銷廣播方法剪决。?
有一個問題灵汪,就是系統(tǒng)自己的音量調(diào)節(jié)提示框也會展示,所以還需要在baseActivity中柑潦,添加一段代碼:
//調(diào)節(jié)音量時享言,不展示系統(tǒng)音量進度條
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
? ? if (mAudioManager ==null) {
? ? ? ? mAudioManager =(AudioManager) this.getSystemService(Service.AUDIO_SERVICE);
? ? }
? ? //小聲鍵
? ? if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
? ? ? ? mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, 0);
return true;
? ? } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
? ? ? ? //大聲鍵
? ? ? ? mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
return true;
? ? }
? ? return super.dispatchKeyEvent( event);
}
這樣就能做到,app中調(diào)大調(diào)小音量渗鬼,系統(tǒng)的音量提示框不展示览露,自己的seekbar展示音量提醒。?
沒法加效果視頻 - - !??