Android学习12-----手机服务(1) 取得电池电量和声音服务:AudioManager

 

一、取得电池电量信息

         Android 系统中,专门提供了一个取得电池电量信息的 Action—ACTION_BATTERY_CHANGED ,在此 Action 中定义了许多附加信息,这些附加信息的名称及作用如下:

No.

附加信息

类型

备注

1

status

int

取得电池的状态,返回的状态类型由 android.os.BatteryManager 类定义的常量所决定,包括:

         ·电池充电状态( BATTERY_STATUS_CHARGING

         ·电池放电状态( BATTERY_STATUS_DISCHARGING

         ·电池满电状态( BATTERY_STATUS_FULL

         ·电池不充电状态( BATTERY_STATUS_NOT_CHARGING

         ·电池未知状态( BATTERY_STATUS_UNKNOWN

2

health

int

取得电池的健康状态,返回的状态类型由 android.os.BatteryManager 类定义的常量所决定,包括:

         ·电池损坏( BATTERY_HEALTH_DEAD

         ·电池健康( BATTERY_HEALTH_GOOD

         ·电池过热( BATTERY_HEALTH_OVERHEAT

         ·电池电压过大( BATTERY_HEALTH_OVER_VOLTAGE

         ·未知状态( BATTERY_HEALTH_UNKOWN

         ·未明示故障( BATTERY_HEALTH_UNSPECIFIED_FAILURE

3

present

boolean

判断当前是否存在电池

4

level

int

取得电池的剩余容量

5

scale

int

取得电池的总容量,通常为 100

6

Icon-small

int

取得电池对应的图标 ID

7

plugged

int

连接的电源插座累心,返回的状态由 android.os.BatteryManager 类定义的常量所决定,包括:

         · USB 电源( BATTERY_PLUGGED_USB

         ·交流电电源( BATTERY_PLUGGED_AC

8

voltage

int

取得电池的电压

9

temperature

int

取得电池的温度,单位是摄氏度

10

technology

String

取得电池的类型

 

范例:取得剩余电量

定义广播接受,显示电池电量, BatteryInfoBroadcastReceiver.java

package com.iflytek.demo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

public class BatteryInfoBroadcastReceiver extends BroadcastReceiver {// 广播接收器

	@Override
	public void onReceive(Context context, Intent intent) {// 接受广播
		if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {// 判断Action
			int level = intent.getIntExtra("level", 0);// 取得电池剩余容量
			int scale = intent.getIntExtra("scale", 100);// 取得电池总理
			Dialog dialog = new AlertDialog.Builder(context)// 创建对话框
					.setTitle("电池电量")// 设置标题
					.setMessage(
							"电池电量为:" + String.valueOf(level * 100 / scale)
									+ "%")// 设置信息
					.setNegativeButton("关闭",
							new DialogInterface.OnClickListener() {// 设置取消按钮
								@Override
								public void onClick(DialogInterface dialog,
										int which) {// 设置监听操作

								}
							}).create();// 创建Dialog
			dialog.show();// 显示对话框
		}
	}

}

 

 BatteryActivity.java

package com.iflytek.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BatteryActivity extends Activity {

	private Button but = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.but = (Button) super.findViewById(R.id.but);
		this.but.setOnClickListener(new OnClickListenerImpl());
	}

	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			BatteryInfoBroadcastReceiver receiver = new BatteryInfoBroadcastReceiver();
			IntentFilter filter = new IntentFilter(
					Intent.ACTION_BATTERY_CHANGED);
			BatteryActivity.this.registerReceiver(receiver, filter);
		}
	}

}
 

 

 

二、声音服务: AudioManager

         Android 系统中,为了满足用户操作声音功能的需要,专门提供了一个声音管理类 —android.media.AudioManager ,通过此类,可以实现手机音量的大小控制,或者进行静音、震动模式的切换。此类中所提供的常量及常用方法如下:

No.

常量及方法

描述

1

Public static final int RINGER_MODE_NORMAL

正常铃声模式

2

Public static final int RINGER_MODE_SILENT

静音模式

3

Public static final int RINGER_MODE_VIBRATE

震动模式

4

Public static final int STAREAM_ALARM

报警音

5

Public static final int STAREAM_MUSIC

播放音乐

6

Public static final int STAREAM_NOTIFICATION

播放提示

7

Public static final int STAREAM_RING

电话铃声

8

Public static final int STAREAM_VOICE_CALL

电话呼叫

9

Public static final int VIBRATE_SETTING_ON

打开震动

10

Public static final int VIBRATE_SETTING_ON

通知震动

11

Public static final int VIBRATE_TYPE_RINGER

电话响铃震动

12

Public static final int ADJUST_LOWER

电话音量调小一格

13

Public static final int ADJUST_RAISE

电话音量调大一格

14

Public int abandonAudioFocus(AudioManager.OnAudioFocusChangeListener l)

放弃声音的焦点并设置监听

15

Public String getParameters(String keys)

返回配置的指定参数内容

16

Public int getRingerMode()

取得响铃模式

17

Public void setParameters(String keyValuePairs)

设置响铃参数

18

Public void setRingerMode(int ringerMode)

设置响铃模式

19

Public void adjustVolume(int direction,int flags)

调节音量

20

Public int getStreamVolume(int streamType)

返回指定数据流的当前音量值

21

Public void setStreamVolume(int streamType,int index,int flags)

设置音频数据流

 

如果想取得 AudioManager 对象,则必须通过 getSystemService() 方法找到 Context.AUDIO_SERVICE 服务。

 

范例:完成设置手机铃声和音量的增加或减少

AudioManagerActivity.java

package com.iflytek.demo;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class AudioManagerActivity extends Activity {

	private ImageButton voiceOn = null;// 打开声音
	private ImageButton voiceOff = null;// 静音按钮
	private ImageButton voiceVibrate = null;// 震动按钮
	private ImageButton voiceLower = null;// 降低音量
	private ImageButton voiceRaise = null;// 调高音量
	private AudioManager audio = null;// 音量管理

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.voiceOn = (ImageButton) super.findViewById(R.id.voiceon);
		this.voiceOff = (ImageButton) super.findViewById(R.id.voiceoff);
		this.voiceVibrate = (ImageButton) super.findViewById(R.id.voicevibrate);
		this.voiceLower = (ImageButton) super.findViewById(R.id.voicelower);
		this.voiceRaise = (ImageButton) super.findViewById(R.id.voiceraise);
		this.audio = (AudioManager) super
				.getSystemService(Context.AUDIO_SERVICE);// 取得服务
		this.voiceOn.setOnClickListener(new VoiceOnOnClickListenerImpl());
		this.voiceOff.setOnClickListener(new VoiceOffOnClickListenerImpl());
		this.voiceVibrate
				.setOnClickListener(new VoiceVibrateOnClickListenerImpl());
		this.voiceLower.setOnClickListener(new VoiceLowerOnClickListenerImpl());
		this.voiceRaise.setOnClickListener(new VoiceRaiseOnClickListenerImpl());
		this.playAudio();
	}

	/**
	 * @descrption 播放音乐
	 * @author xdwang
	 * @create 2012-11-05 下午8:55:38
	 */
	private void playAudio() {
		MediaPlayer media = MediaPlayer.create(this, R.raw.shiqisuideyuji);// 指定资源
		media.setLooping(true);// 循环播放
		try {
			media.prepare();// 预备状态
		} catch (Exception e) {
		}
		media.start();// 播放文件
	}

	private class VoiceOnOnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			AudioManagerActivity.this.audio
					.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
			Toast.makeText(AudioManagerActivity.this, "手机音量开启!",
					Toast.LENGTH_SHORT).show();
		}

	}

	private class VoiceOffOnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			AudioManagerActivity.this.audio
					.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			Toast.makeText(AudioManagerActivity.this, "手机静音!",
					Toast.LENGTH_SHORT).show();
		}

	}

	private class VoiceVibrateOnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			AudioManagerActivity.this.audio
					.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
			Toast.makeText(AudioManagerActivity.this, "手机为震动模式!",
					Toast.LENGTH_SHORT).show();
		}

	}

	private class VoiceLowerOnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			AudioManagerActivity.this.audio.adjustVolume(
					AudioManager.ADJUST_LOWER, 0);
			Toast.makeText(AudioManagerActivity.this, "音量减小!",
					Toast.LENGTH_SHORT).show();
		}

	}

	private class VoiceRaiseOnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			AudioManagerActivity.this.audio.adjustVolume(
					AudioManager.ADJUST_RAISE, 0);
			Toast.makeText(AudioManagerActivity.this, "音量增加!",
					Toast.LENGTH_SHORT).show();
		}

	}
}

你可能感兴趣的:(adroid,AudioManager,BatteryManager)