双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

android监听usb拔插事件

Kexincy Kexincy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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及其之前的东西。
...全文
评论 10
收藏 已收藏
分享
返回顶部
Kexincy
Android 80,489 成员 91,375 内容