调节系统的亮度
package com.example.demo2; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.Settings; import android.view.KeyEvent; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.ProgressBar; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; /** * Created by on 2017/1/4. * 控制亮度的dialog */ public class LightDialog extends Dialog { private static final int LIGHT_UP = 1; private static final int LIGHT_DOWN = 2; private Context context; private ProgressBar pb_loading; private TextView tv; private ProgressBar pb_light; private Integer tmpInt; public LightDialog(Context context) { super(context); this.context = context; } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case LIGHT_UP: tv.setText("当前亮度为"+tmpInt); break; case LIGHT_DOWN: tv.setText("当前亮度为"+tmpInt); break; case 0: tv.setText("当前亮度为"+pb_light.getProgress()); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_light); pb_loading = (ProgressBar) findViewById(R.id.pb_loading); tv = (TextView) findViewById(R.id.tv); pb_light = (ProgressBar) findViewById(R.id.pb_light); updateToggles(); handler.sendMessageDelayed(handler.obtainMessage(0),0); } private void updateToggles() { pb_light.setProgress((int) (android.provider.Settings.System.getInt( context.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 255) )); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode){ case KeyEvent.KEYCODE_3: //获取当前的progress的进度值 tmpInt = pb_light.getProgress(); //点击之后将数值添加51 为一个系统的一个亮度值 if (tmpInt==255){ Toast.makeText(context, "已经是最大值了", Toast.LENGTH_SHORT).show(); }else { tmpInt = tmpInt + 51; } //增加之后打印出来 System.out.println(tmpInt); // TODO 51 (seek scale) * 5 = 255 (max brightness) //将值设置给Progress pb_light.setProgress(tmpInt); //将值设置给系统亮度显示 android.provider.Settings.System.putInt(context.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, tmpInt); // 0-255 handler.sendMessageDelayed(handler.obtainMessage(LIGHT_UP),0); break; case KeyEvent.KEYCODE_4: //获取当前的progress的进度值 tmpInt = pb_light.getProgress(); //点击之后将数值添加51 为一个系统的一个亮度值 if (tmpInt==0){ Toast.makeText(context, "已经是最小值", Toast.LENGTH_SHORT).show(); }else { tmpInt = tmpInt - 51; } //增加之后打印出来 System.out.println(tmpInt); // TODO 51 (seek scale) * 5 = 255 (max brightness) //将值设置给Progress pb_light.setProgress(tmpInt); //将值设置给系统亮度显示 android.provider.Settings.System.putInt(context.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, tmpInt); // 0-255 handler.sendMessageDelayed(handler.obtainMessage(LIGHT_DOWN),0); break; } return super.onKeyDown(keyCode, event); } }
//调节系统的声音
package com.example.demo2; import android.app.Dialog; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; /** * Created by 刘新 on 2017/1/3. * 创建对话框 * 设置系统的声音 */ public class SoundDialog extends Dialog { private static final int VOICE_UP = 1; private static final int VOICE_DOWN = 2; public Context context; private Uri notification; private Ringtone r; private MediaPlayer mediaPlayer; public SoundDialog(Context context) { super(context); this.context = context; } //进度条显示 private ProgressBar progressBar; //加载进度条 private ProgressBar pb_loading; private TextView tv; private int currentVolume;//系统当期的音量 private AudioManager audioManager; //创建handler更新界面 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case VOICE_UP: tv.setText("音量级别为" + currentVolume); progressBar.setProgress(currentVolume); break; case VOICE_DOWN: tv.setText("音量级别为" + currentVolume); progressBar.setProgress(currentVolume); break; case 0: pb_loading.setVisibility(View.INVISIBLE); Log.i("打印",currentVolume+""); tv.setText("音量级别为"+currentVolume); progressBar.setProgress(currentVolume); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sound); progressBar = (ProgressBar) findViewById(R.id.progressBar); tv = (TextView) findViewById(R.id.tv); audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); pb_loading = (ProgressBar) findViewById(R.id.pb_loading); pb_loading.setVisibility(View.VISIBLE); //获取当前的系统音量 currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM); handler.sendMessageDelayed(handler.obtainMessage(0),2000); } //设置系统 的键盘的按键调节 系统的音量 //TODO 播放的时候判断是不是正在播放 如果播放中 停止 重新开始播放 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode){ case KeyEvent.KEYCODE_1: audioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM,AudioManager.ADJUST_RAISE , AudioManager.FLAG_VIBRATE); currentVolume = audioManager.getStreamVolume(1); handler.sendMessageDelayed(handler.obtainMessage(VOICE_UP),0); mediaPlayer = MediaPlayer.create(context, R.raw.ring1); mediaPlayer.setLooping(false); if (mediaPlayer.isPlaying()){ mediaPlayer.stop(); }else { mediaPlayer.start();//播放 } break; case KeyEvent.KEYCODE_2: audioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM,AudioManager.ADJUST_LOWER , AudioManager.FLAG_VIBRATE); currentVolume = audioManager.getStreamVolume(1); handler.sendMessageDelayed(handler.obtainMessage(VOICE_DOWN),0); mediaPlayer = MediaPlayer.create(context, R.raw.ring1); mediaPlayer.setLooping(false); mediaPlayer.start();//播放 break; } return true; } }
//主程序
package com.example.demo2; import android.app.Activity; import android.app.Dialog; import android.media.AudioManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { private Button btn; private Button voice_up; private Button voice_down; private Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Dialog dialog = new SoundDialog(MainActivity.this); dialog.setTitle("音量设置"); dialog.show(); } }); btn2 = (Button) findViewById(R.id.btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Dialog dialog = new LightDialog(MainActivity.this); dialog.setTitle("亮度设置"); dialog.show(); } }); } }