2011年8月18日星期四

實施廣播接收器(BroadcastReceiver)檢測USB大容量存儲模式(USB Mass Storage mode)

檢測USB大容量存儲(USB Mass Storage)的連接(Connected)和斷開(Disconnected)模式之間的轉換, 我們可以擴展廣播接收器(BroadcastReceiver), 處理 Intent.ACTION_UMS_CONNECTED 和 Intent.ACTION_UMS_DISCONNECTED 事件. 此事件會在通過 USB 連接和斷開時發生.

實施廣播接收器(BroadcastReceiver)檢測USB大容量存儲模式(USB Mass Storage mode)

實例:
package com.AndroidUSB;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidUSBActivity extends Activity {

TextView umsState;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
umsState = (TextView)findViewById(R.id.ums_state);

registerReceiver(receiverUmsConnected, new IntentFilter(Intent.ACTION_UMS_CONNECTED));
registerReceiver(receiverUmsDisConnected, new IntentFilter(Intent.ACTION_UMS_DISCONNECTED));

}

BroadcastReceiver receiverUmsConnected
= new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
umsState.setText("USB Mass Storage Connected");
}};

BroadcastReceiver receiverUmsDisConnected
= new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
umsState.setText("USB Mass Storage Disconnected");

}};
}





沒有留言:

發佈留言