android监听usb拔插事件
public class UsbActivity extends Activity {
/** Called when the activity is first created. */
private BroadcastReceiver receiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
receiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("+++++++++++","action="+intent.getAction());
Toast.makeText(UsbActivity.this, "action-----"+intent.getAction(), Toast.LENGTH_SHORT).show();
}
};
IntentFilter filter=new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_CHECKING);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addAction(Intent.ACTION_MEDIA_SHARED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
registerReceiver(receiver,filter);
}
public void onDestroy(){
super.onDestroy();
unregisterReceiver(receiver);
}
}
这是我监听USB拔插事件的程序,在平板上运行的时候,没有任何toast出现,请问问题出在哪?action正确吗?
另外usb的action只在3.1才开始有,现在我要用的是3.0及其之前的东西。