Android U盘插拔监听详细版以及U盘写入权限解决办法


源码下载:

https://download.csdn.net/download/qq_31939617/10436607下载

先看效果:
这里写图片描述
用广播接收U盘插板的状态,其中插板的判断可以加入
“android.intent.action.MEDIA_MOUNTED”.equals(action)
&& “/mnt/usbhost1”.equals(path)
其中”/mnt/usbhost1”是我修改过的U盘的挂载路径,根据你们自己系统的路径进行修改。至于这个路径各个机器可能不一样,注意看下面:

先上代码:
MainAcitvity.class

package com.example.sz.usbdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean b = new USBDiskState().isMounted();
        Log.e(TAG, "onCreate: ------U盘是否存在-------" + b);
    }


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">

    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

USBDiskReceiver .class

package com.example.sz.usbdemo;

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

public class USBDiskReceiver extends BroadcastReceiver {
    private static final String TAG = "USBDiskReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String path = intent.getData().getPath();
        if (!TextUtils.isEmpty(path)) {
            if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)) {
                Log.e(TAG, "onReceive: ---------------usb拨出-------------");
            }
            if ("android.intent.action.MEDIA_MOUNTED".equals(action)) {
                Log.e(TAG, "onReceive: --------usb路径-------"+ path);

            }
        }
    }


}

**先用广播的方式得到U盘的路径:
Log.e(TAG, “onReceive: ——–usb路径——-“+ path);**

用上面方法是没有办法判断U盘是不是已经挂载上了,比如开机前别人就插上了U盘。
如果是这样,就用另外一个方法,原理是U盘在挂载上的时候会在/proc/mounts里写入U盘的挂载路径,所以我的path传入”/mnt/usbhost1”即可判定,这个路径,就是在广播的时候得到的

USBDiskState.class

package com.example.sz.usbdemo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class USBDiskState {
    private static final String MOUNTS_FILE = "/proc/mounts";

    private static String path = "/mnt/usbhost1";

    public static boolean isMounted() {

        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;
    }


}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sz.usbdemo">

    <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>

    <application  android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".USBDiskReceiver">
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.MEDIA_MOUNTED" />
                <action android:name="android.intent.action.MEDIA_UNMOUNTED" />

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

</manifest>

源码下载:

https://download.csdn.net/download/qq_31939617/10436607下载


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号