Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 聽筒模式 實現

Android 聽筒模式 實現

編輯:關於Android編程

一、利用距離感應器監聽聽筒靠近耳朵事件

准確的說距離感應器並不能監聽到你是否把手機靠近耳朵,也許是你用手擋在了距離感應器前面,但這不是我們關心的,我們關心的是當你的耳朵靠近聽筒時,我們要捕獲到這個事件

step 1,新建實現SensorEventListener接口的類並實現onSensorChanged(SensorEvent event)方法

public class MainActivity extends Activity implements SensorEventListener

step 2,得到距離感應器的實例

audioManager = (AudioManager) this
				.getSystemService(Context.AUDIO_SERVICE);
		mSensorManager = (SensorManager) getSystemService


(Context.SENSOR_SERVICE);
		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

step 3,在onSensorChanged方法中判斷是否靠近聽筒

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		float range = event.values[0];


		if (range == mSensor.getMaximumRange()) {
			Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(this, "聽筒模式", Toast.LENGTH_LONG).show();
		}


二、根據距離感應器改變AudioManager播放模式

默認情況下,播放語音都是用外置喇叭或者耳機,我們需要編碼實現播放的切換。

step 1,在AndroidManifest文件中添加權限聲明


step 2,切換播放模式

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		float range = event.values[0];


		if (range == mSensor.getMaximumRange()) {
			Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
			 audioManager.setMode(AudioManager.MODE_NORMAL);
		} else {
			Toast.makeText(this, "聽筒模式", Toast.LENGTH_LONG).show();
			 audioManager.setMode(AudioManager.MODE_IN_CALL);
		}

完整代碼:

package com.markjoker.sensortest;

import java.io.FileInputStream;
import java.io.IOException;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {
	private AudioManager audioManager;
	private SensorManager mSensorManager;
	private Sensor mSensor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		audioManager = (AudioManager) this
				.getSystemService(Context.AUDIO_SERVICE);
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
		
		MediaPlayer mPlayer = new MediaPlayer();
		try {
			mPlayer.reset();
			mPlayer.setDataSource(new FileInputStream("/sdcard/snow.mp3").getFD());
			mPlayer.prepare();
			mPlayer.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onResume() {
		mSensorManager.registerListener(this, mSensor,
				SensorManager.SENSOR_DELAY_NORMAL);
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		mSensorManager.unregisterListener(this);
		super.onPause();
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		float range = event.values[0];

		if (range == mSensor.getMaximumRange()) {
			Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
			 audioManager.setMode(AudioManager.MODE_NORMAL);
		} else {
			Toast.makeText(this, "聽筒模式", Toast.LENGTH_LONG).show();
			 audioManager.setMode(AudioManager.MODE_IN_CALL);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved