第一部分 传感器
传感器是一种物理装置或生物器官,能够探测、感受外界的信号、物理条件(如:光、热、湿度)或化学组成(如:烟雾),并将探知的信息传递给其他设备或器官。
Android支持的传感器有如下几种:
Sensor.TYPE_ACCELEROMETER 加速度传感器
Sensor.TYPE_GYROSCOPE 陀螺仪传感器
Sensor.TYPE_LIGHT 亮度传感器
Sensor.TYPE_MAGNETIC_FIELD 地磁传感器
Sensor.ORIENTATION 方向传感器
Sensor.TYPE_PRESSURE 压力传感器
Sensor.TYPE_PROXIMITY 近程传感器
Sensor.TYPE_TEMPERATURE 温度传感器
Android中传感器 (此特效必须在真机中才能看到效果)
Android传感器的使用需要掌握SensorManager和SensorEventListener
SensorManager 就是传感器的一个综合管理类
SensorManager mSensorManager = (SensorManger)getSystemService(SENSOR_SERVICE);//通过getSystemService得到SensorManager对象
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);//此处通过getSensorList获得我们需要的传感器类型,并保存到一个传感器列表中
Boolean mRegisteredSensor = mSensorManager.registerListener(this,sensors,SensorManager.SENSOR_DELAY_FASTEST);//通过registerListener注册一个监听器其中
方法中参数 this---接收信号的Listener
sensors---接收传感器类型的列表即上一步的List<Sensor>
SensorManager.SENSOR_DELAY_FASTEST---接收频度
此方法调用之后返回一个boolean值,true为成功 ,false为失败
mSensorManager.unregisterListener(this);//在不使用时需要通过unregisterListener卸载传感器
SensorEventListener 传感器的核心部分 以下两个方法必须实现
onSensorChanged(SensorEvent event) 此方法在传感器值更改时调用。该方法只由受此应用程序监视的传感器调用。
其中参数为SensorEvent对象,该对象包括一组浮点数,表示传感器获得的方向、加速度等信息如下:
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
onAccuracyChanged(Sensor sensor,int accuracy) 此方法在传感器的精准度发生改变时调用。
其中参数 第一个表示传感器 第二个表示传感器新的准确值
除上两种重要方法外还有以下几种
getDefaultSensor 得到默认的传感器对象
getInclination 得到地磁传感器倾斜角的弧度制
getOrientation 得到设备选择的方向
getSensorList 得到指定传感器列表
下例为实现了一个方向传感器的例子 实现了如何获得传感器的方向,加速度等信息,可以根据得到的数值与上一次得到的数值之间的关系进行需要操作。
public class Activity01 extends Activity implements SensorEventListener{
private boolean mRegisteredSensor;
private SensorManager mSensorManager;//定义SensorManager
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRegisteredSensor = false;
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//取得SensorManager实例
}
protected void onResume(){
super.onResume();
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);//接受SensorManager的一个列表(Listener),这里我们指定类型为TYPE_ORIENTATION(方向感应器)
if (sensors.size() > 0){
Sensor sensor = sensors.get(0);
mRegisteredSensor = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);//注册SensorManager,this->接收sensor的实例,第二个参数为接收传感器类型的列表,第三个参数为接受的频率
}
}
protected void onPause(){
if (mRegisteredSensor){
mSensorManager.unregisterListener(this);//通过unregisterListener来卸载\取消注册
mRegisteredSensor = false;
}
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy){//当进准度发生改变时调用此方法,sensor->传感器,accuracy->精准度
}
public void onSensorChanged(SensorEvent event){// 此方法在传感器在被改变时触发
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION){// 接受方向感应器的类型
//这里我们可以得到数据,然后根据需要来处理,由于模拟器上面无法测试效果,因此我们暂时不处理数据
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
}
}
}
第二部分 语音识别
Android中通过RecognizerIntent来实现语音识别。
RecognizerIntent包含以下常量:
ACTION_RECOGNIZE_SPEECH 开启语音活动
ACTION_WEB_SEARCH 开启网络语音模式,结果将以网页搜索显示
EXTRA_LANGUAGE 设置一个语言库
EXTRA_LANGUAGE_MODEL 语音识别模式
EXTRA_MAX_RESULTS 返回最大的结果
EXTRA_PROMPT 提示用户可以开始语音了
EXTRA_RESULTS 将字符串返回到一个ArrayList中
LANGUAGE_MODEL_FREE_FORM 在一种语言模式上自由语音
LANGUAGE_MODEL_WEB_SEARCH 使用语音模式在Web上搜索
RESULT_AUDIO_ERROR 返回结果时,音频遇到错误
RESULT_CLIENT_ERROR 返回结果时,客户端遇到错误
RESULT_NETWORK_ERROR 返回结果时,网络遇到错误
RESULT_NO_MATCH 没有检测到语音的错误
RESULT_SERVER_ERROR 返回结果时,服务器遇到错误
在使用语音识别时需要通过Intent来传递一个动作以及一些属性,然后通过startActivityForResult来开始语音 代码如下:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RcognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"开始语音");
startActivityForResult(intent ,VOICE_RECOGNITION_REQUEST_CODE);//通过startActivityForResult来开始语音
另外还需要实现onActivityResult方法,当语音接收时,会触发来获得语音的字符序列。
下例中我们实现了 点击按钮时开始语音,并在onActivityResult方法中取得结果并显示出来
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Button01"
android:text="开始使用语音识别"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
public class Activity01 extends Activity{
private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;
private ListView mList;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mList = (ListView) findViewById(R.id.ListView01);
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
try{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);//通过Intent传递语音识别的模式,开启语音
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);//语言模式和自由形式的语音识别
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"开始语音");//提示语音开始
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);//开始执行我们的Intent、语音识别
}catch (ActivityNotFoundException e){//如果找不到设置,就会抛出ActivityNotFoundException
Toast.makeText(Activity01.this,"ActivityNotFoundException", Toast.LENGTH_LONG).show(); //找不到语音设备装置
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){//当语音结束时的回调函数onActivityResult
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK){// 判断是否是我们执行的语音识别
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);// 取得语音的字符
//设置视图更新
//mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
String resultsString = "";
for (int i = 0; i < results.size(); i++){
resultsString += results.get(i);
}
Toast.makeText(this, resultsString, Toast.LENGTH_LONG).show();
super.onActivityResult(requestCode, resultCode, data);
}
}
}
第三部分 账户管理
Android中通过android.accounts包中所包含的集中式的账户管理API,用于安全地存储和访问认证的令牌和密码。
android.accounts包中的功能:
AccountManagerCallback 账户管理回调接口
AccountManagerFuture 代表一个AccountMananger的异步调用结果
OnAccountUpdateListener 账户监听回调接口
AbstractAccountAuthenticator 创建AccountAuthenticators(账户验证)的一个基类
Account AccountManager中的一个账户信息及类型
AccountAuthenticatorActivity 用于实现一个AbstractAccountAuthenticator的活动
AccountAuthenticatorResponse 账户验证响应
AccountManager 账户管理器
AuthenticatorDescription 一个Parcelable类型的值包括了账户验证的信息
mAccountManager = AccountManager.get(this);//通过AccountManager类的get方法获得AccountManager对象
AccountManager中的常用方法
addAccount 添加一个账户
addOnAccountUpdatedListenenr 添加一个账户更新的监听器
removeOnAccountsUpdatedListener 取消一个账户更新的监听器
clearPassword 清除指定账户的密码数据
getAccounts 得到所有的账户信息
getAccountsByType 得到指定类型的账户信息
getPassword 得到指定账户的密码
setUserData 设置指定账户的信息
setPassword 设置指定账户的密码
removeAccount 删除一个账户
public class SleepyAccountAuthenticatorActivity extends AccountAuthenticatorActivity{//此类供用户输入信息
protected void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.new_account);
final Button done = (Button) findViewById(R.id.new_account_done);
final EditText server = (EditText) findViewById(R.id.new_account_server);
final EditText username = (EditText) findViewById(R.id.new_account_username);
final EditText password = (EditText) findViewById(R.id.new_account_password);
final Activity self = this;
done.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Account account = new Account(username.getText().toString(), getString(R.string.ACCOUNT_TYPE));//通过指定账户名和账户类型构建一个Account对象
Bundle userdata = new Bundle(); //将服务器数据通过Bundle方式加入进来
userdata.putString("SERVER", server.getText().toString());
AccountManager am = AccountManager.get(self);//取得AccountManager
if (am.addAccountExplicitly(account, password.getText().toString(), userdata)){//通添addAccountExplicitly向账户管理器中添加一个账户信息
Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, username.getText().toString());
result.putString(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.ACCOUNT_TYPE));
setAccountAuthenticatorResult(result);
}
finish();
}
});
}
}
public class SleepyAccountAuthenticator extends AbstractAccountAuthenticator{//在添加、操作账户信息时会通过AbstractAccountAuthenticator实现异步调用
private String _tag = "SleepyAccountAuthenticator";
private Context _context;