android中对u盘以及tf卡插入的广播监听
来源:互联网 发布:plm软件占有率 编辑:程序博客网 时间:2024/06/06 01:09
public class UsbStateReceiver extends BroadcastReceiver { //BroadcastReceiver private String TAG = "UsbStateReceiver"; public static final int USB_STATE_ON = 0x00021; public static final int USB_STATE_OFF = 0x00022; private static final int GET_RUNNING_TASKS_NUM = 30; private static final String PACKAGE_NAME = "com.memo.filemanager"; private static Handler mHandler; public static Handler getHandler() { return mHandler; } public static void setHandler(Handler handler) { mHandler = handler; } private static int number = 0; @Override public void onReceive(final Context context, Intent intent) { if ( number++ == 0) { return; } Message msg = new Message(); String action = intent.getAction(); if (action.equals(Intent.ACTION_MEDIA_EJECT)) { //USB设备移除,更新UI Log.d( TAG, "USB:ACTION_MEDIA_EJECT"); msg.what = USB_STATE_OFF; } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { //USB设备挂载,更新UI Log.d( TAG, "USB:ACTION_MEDIA_MOUNTED"); msg.what = USB_STATE_ON; ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); RunningTaskInfo taskinfo = am.getRunningTasks(1).get(0); // 获取到当前正在栈顶运行的Activity。 String packname = taskinfo.topActivity.getPackageName(); // 获取到当前任务栈顶程序所对应的包名。 if( !packname.equals(PACKAGE_NAME) ) { /* final MyDialog confirmDialog = new MyDialog(context, "使用文件管理器打开U盘?", "确定", "取消"); confirmDialog.setClicklistener(new MyDialog.ClickListenerInterface() { @Override public void doConfirm() { confirmDialog.dismiss(); Intent i = new Intent(context, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.putExtra("Selected", "Removable Disk"); context.startActivity(i); } @Override public void doCancel() { confirmDialog.dismiss(); } }); confirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); confirmDialog.show(); */ Intent i = new Intent(context, MainActivity.class); if( isRunningTask(context)) { i.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); } else { i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } i.putExtra("Selected", "Removable Disk"); context.startActivity(i); } } if( mHandler != null ) { mHandler.sendMessage(msg); } } public boolean isRunningTask(Context context){ ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks= am.getRunningTasks(GET_RUNNING_TASKS_NUM); Iterator<RunningTaskInfo> itInfo = tasks.iterator(); while( itInfo.hasNext() ){ RunningTaskInfo info = itInfo.next(); String packname = info.baseActivity.getPackageName(); if( packname.equals(PACKAGE_NAME) ) { return true; } } return false; } public boolean isRunningProcess( Context context ) { PackageManager pm = context.getPackageManager(); ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcessList = am.getRunningAppProcesses(); ApplicationInfo appInfo = null; for (RunningAppProcessInfo appProcessInfo : appProcessList) { try { appInfo = pm.getApplicationInfo(appProcessInfo.processName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if(appInfo.packageName.equals(PACKAGE_NAME)) { return true; } } return false; }}
0 0
- android中对u盘以及tf卡插入的广播监听
- android 广播监听 U盘插入与拔出
- Android 监听外部U盘插入
- android中监听wifi广播的方法
- 对U盘插入消息的响应
- 对Android中广播的理解
- android中对broadcast广播的利用
- Android-->电话广播的监听
- android获取扩展存储路径,TF卡、U盘等
- android中对sim卡联系人的增删改查以及监听sim卡联系数据的改变
- Android中对手机的返回键,Home键以及多功能切换键的监听
- Android中对音量的监听控制
- android中对按钮的监听
- 广播的接收与U盘广播
- 广播的接收与U盘广播
- Android U盘插拔监听详细版以及U盘写入权限解决办法
- VB 监听U盘插入并拷贝U盘内容
- Android apk安装监听的几个广播
- 【python3自学笔记3】—字符串和编码
- Decorrelating Semantic Visual Attributes by Resisting the Urge to Share 论文笔记
- 如何讲man帮助手册改成中文的
- JS实现的文本框内容发生改变立马触发事件简介
- 211. Add and Search Word - Data structure design
- android中对u盘以及tf卡插入的广播监听
- .NET使用OpenSSL生成的pem密钥文件
- Spoken Attributes: Mixing Binary and Relative Attributes to Say the Right Thing 论文笔记
- 携程移动端架构演进与优化之路
- Unity3d 跨平台原理
- ExtJS textField 表单验证 常用正则表达式
- Oracle建立表空间和用户
- IntelliJ IDEA 使用心得与常用快捷键
- MSE和PSNR