系统设置——音量调节

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

权限要添加,不然会报  java.lang.SecurityException: Not allowed to change Do Not Disturb state   异常

 

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.zl.myapplication.R;

public class SoundActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    public AudioManager audiomanage;
    private int maxVolume, currentVolume,everVolume;
    boolean isVoice = false;//true:有声音

    SeekBar seekbar_voice;
    private ImageView img_voice;
    private TextView tv_voice;
    private LinearLayout ll_voice;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);

        // 这块是关于权限的,一定要添加,不然也会报同上的异常
        NotificationManager mNotificationManager =
                (NotificationManager) SoundActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
                && !mNotificationManager.isNotificationPolicyAccessGranted()) {
            Intent intent_v = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
            SoundActivity.this.startActivity(intent_v);
        }

        /**
         * 声音调节
         */
        audiomanage = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        //获取系统最大音量
        maxVolume = audiomanage.getStreamMaxVolume(AudioManager.RINGER_MODE_NORMAL);
        //获取当前值
        currentVolume = audiomanage.getStreamVolume(AudioManager.RINGER_MODE_NORMAL);
        everVolume = currentVolume;
        seekbar_voice = findViewById(R.id.seekbar_voice);
        seekbar_voice.setMax(maxVolume);
        seekbar_voice.setProgress(currentVolume);
        seekbar_voice.setOnSeekBarChangeListener(this);

        img_voice = findViewById(R.id.img_voice);
        tv_voice = findViewById(R.id.tv_voice);
        ll_voice = findViewById(R.id.ll_voice);
        ll_voice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickVoice();
            }
        });
        int ringerMode = audiomanage.getRingerMode();
        if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
            //表示当前是震动,暂时还没有图标,设为静音同
            img_voice.setImageResource(R.drawable.shengyin);
            tv_voice.setTextColor(getResources().getColor(R.color.black));
            isVoice = false;
        } else if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
            //表示当前是静音
            img_voice.setImageResource(R.drawable.shengyin);
            tv_voice.setTextColor(getResources().getColor(R.color.black));
            isVoice = false;
        } else {
            img_voice.setImageResource(R.drawable.shengyin_h);
            tv_voice.setTextColor(getResources().getColor(R.color.text_click));
            isVoice = true;
        }

    }

    private void clickVoice() {
        if (isVoice) {
            everVolume = currentVolume;
            img_voice.setImageResource(R.drawable.shengyin);
            tv_voice.setTextColor(getResources().getColor(R.color.black));
            audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            currentVolume = audiomanage.getStreamVolume(AudioManager.RINGER_MODE_NORMAL);  //获取当前值
            seekbar_voice.setProgress(currentVolume);
            isVoice = false;
        } else {
            img_voice.setImageResource(R.drawable.shengyin_h);
            tv_voice.setTextColor(getResources().getColor(R.color.text_click));
            audiomanage.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            audiomanage.setStreamVolume(AudioManager.RINGER_MODE_NORMAL, everVolume, 0);
            currentVolume = audiomanage.getStreamVolume(AudioManager.RINGER_MODE_NORMAL);  //获取当前值
            seekbar_voice.setProgress(currentVolume);
            isVoice = true;
        }
    }

    /**
     * Seekbar滚动时的回调函数
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        audiomanage.setStreamVolume(AudioManager.RINGER_MODE_NORMAL, progress, 0);
        currentVolume = audiomanage.getStreamVolume(AudioManager.RINGER_MODE_NORMAL);  //获取当前值
        seekbar_voice.setProgress(currentVolume);
        if (progress > 0 && !isVoice) {
            //表示原来是静音的,现在调节有声音了
            isVoice = true;
            img_voice.setImageResource(R.drawable.shengyin_h);
            tv_voice.setTextColor(getResources().getColor(R.color.text_click));
        } else if (progress == 0 && isVoice) {
            //表示当前是静音,但原来是有声音的
            isVoice = false;
            img_voice.setImageResource(R.drawable.shengyin);
            tv_voice.setTextColor(getResources().getColor(R.color.black));
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
R.layout.activity_sound
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/shengyintiaoshi_di" />

        <SeekBar
            android:id="@+id/seekbar_voice"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:max="100"
            android:maxHeight="4dp"
            android:minHeight="4dp"
            android:progress="10"
            android:progressDrawable="@drawable/po_seekbar"
            android:thumb="@drawable/seekbar_thumb" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/shengyintiaoshi_gao" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_voice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img_voice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/shengyin" />

        <TextView
            android:id="@+id/tv_voice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="12dp"
            android:text="声音"
            android:textColor="#000"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

 shengyin.png                shengyin_h.png

  shengyintiaoshi_di.png          shengyintiaoshi_gao.png

 

@drawable/po_seekbar
<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#5000" />
            <corners android:radius="10dp"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#5000" />
                <corners android:radius="10dp"/>
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#417ea5" />
                <corners android:radius="2dp"/>
            </shape>
        </clip>
    </item>
</layer-list>
@drawable/seekbar_thumb
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/dian_blue"
        android:state_focused="true"
        android:state_pressed="false" />
    <item
        android:drawable="@drawable/dian_blue"
        android:state_focused="true"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/dian_blue"
        android:state_focused="false"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/dian_blue" />
</selector>

  dian_blue.png

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值