android u盘两种方式使用(广播和挂载)

该资料通过网上资料总结:

1.通过检测

String path = "/storage/uhost2";
boolean mounted = isMounted(path);

private static final String MOUNTS_FILE = "/proc/mounts";

public static boolean isMounted(String path) {
    boolean blnRet = false;
    String strLine = null;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(MOUNTS_FILE));

        while ((strLine = reader.readLine()) != null) {
            if (strLine.contains(path)) {
                blnRet = true;
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            reader = null;
        }
    }
    return blnRet;
}

2.通过广播方式检测

广播方面

package com.wuyue.horld;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.io.File;

public class USBReceiver extends BroadcastReceiver {

    private static final String TAG = USBReceiver.class.getSimpleName();
    private BRInteraction brInteraction;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
            String mountPath = intent.getData().getPath();
            Log.d(TAG, "mountPath = " + mountPath);
            if (!TextUtils.isEmpty(mountPath)) {
                //读取到U盘路径再做其他业务逻辑
                //发送广播
                File file = new File(mountPath + "/" + "111.zip");
                if (file.exists()) {
                    file.delete();
                }
                File file1 = new File(mountPath + "/" + "FINGER_MARKS/");
                if (file1.exists()) {
                    file1.delete();
                }
                try {
                    ZipUtil.zipFolder("data/data/" + context.getPackageName() + "/" + "FINGER_MARKS/", mountPath + "/" + "111.zip");
                    Toast.makeText(context, "压缩成功", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED) || action.equals(Intent.ACTION_MEDIA_EJECT)) {
            Toast.makeText(context, "No services information detected !", Toast.LENGTH_SHORT).show();
        } else if (action.equals("android.intent.action.BOOT_COMPLETED")) {
            //如果是开机完成,则需要调用另外的方法获取U盘的路径
            Toast.makeText(context, "请重新插入u盘", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, "请重新插入u盘", Toast.LENGTH_SHORT).show();
        }


    }

    public interface BRInteraction {
        public void setText(String content);
    }

    public void setBRInteractionListener(BRInteraction brInteraction) {
        this.brInteraction = brInteraction;
    }
}

界面

btzip.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //方式1   通过广播的形式进行zip到指定u盘
         intentFilter = new IntentFilter();
     	 intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        receiver = new USBReceiver();
        registerReceiver(receiver, intentFilter);
    
    }
});

广播清单文件

<receiver android:name="com.wuyue.horld.USBReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.MEDIA_REMOVED" />
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <action android:name="android.intent.action.MEDIA_REMOVED" />
        <action android:name="android.intent.action.MEDIA_REMOVED" />

        <data android:scheme="file"></data>
    </intent-filter>
</receiver>

3 linux 得到u盘信息

https://github.com/alt236/USB-Device-Info---Android  

 
 

猜你喜欢

转载自blog.csdn.net/qqda6/article/details/78589878
今日推荐