Android监听U盘插入,自动查找文件

定义要查找的文件名称

    private static final String fileName_MP4 = "xxx.MP4";
    private static final String fileName_mp4 = "xxx.mp4";

注册U盘插入广播

    public CheckVideoByDisk(Context context){
        mContext = context;

        IntentFilter usbfilter = new IntentFilter();
        usbfilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        usbfilter.addDataScheme("file");
        mContext.registerReceiver(usbReceiver, usbfilter);
    }

定义广播接收器

    BroadcastReceiver usbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals(Intent.ACTION_MEDIA_MOUNTED)){
                Log.d(TAG, "usb plug in, start play!!");
                if(!isBoot) {
                    mHandler.removeMessages(MSG_CHECK_BOOT);
                    mHandler.sendEmptyMessageDelayed(MSG_CHECK_BOOT, 10*1000);
                }else {
                    mHandler.removeMessages(MSG_CHECK_BOOT);
                    mHandler.sendEmptyMessageDelayed(MSG_CHECK_BOOT, 2*1000);
                }
            }
        }
    };

当接收到U盘插入广播之后使用handler机制,检测U盘文件

    private Handler mHandler = new Handler(){
        @java.lang.Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == MSG_CHECK_BOOT){
                checkUsbFile();
                isBoot = true;
            }
        }
    };

创建一个线程,去扫描读取U盘指定文件

    private void checkUsbFile(){
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(2000);
                    String filePath = findFileOnUSB(fileName_mp4);
                    if(filePath.equals("")){
                        Log.d(TAG,"can't find xxxx.MP4 in USB!");
                        String filePath_mp4 = findFileOnUSB(fileName_MP4);
                        if(filePath_mp4.equals("")){
                            Log.d(TAG,"can't find xxxx.MP4 in USB!");
                        }else{
                            viewFile(mContext, filePath);
                        }
                    }else{
                        viewFile(mContext, filePath);
                    }
                } catch (Exception e) {
                    Log.e(TAG, e.toString());
                }
            }
        }.start();
    }

找到该视频文件之后,使用MTK自带播放器播放

    private static void viewFile(final Context context, final String filePath) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String type = "video/*";
        File file = new File(filePath);
        Uri name = Uri.fromFile(file);
        intent.setDataAndType(name, type);
        intent.setClassName("com.mediatek.wwtv.mediaplayer", "com.mediatek.wwtv.mediaplayer.mmp.multimedia.MtkFilesGridActivity");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        context.startActivity(intent);
    }


   

你可能感兴趣的:(Android笔记,android)