前言:在rk的主板上,因为是定制的东西,所以有些东西跟原生的有些不一样,比如监听外部设备的拔插以及地址获取
不废话了直接上代码:下面的是一个广播
-
package com.example.receiver;
-
-
import java.io.File;
-
import java.util.ArrayList;
-
import java.util.List;
-
import com.example.tfsd.Singleton;
-
import com.example.utrl.ReadAssetsUtils;
-
-
import android.annotation.SuppressLint;
-
import android.content.BroadcastReceiver;
-
import android.content.Context;
-
import android.content.Intent;
-
import android.content.IntentFilter;
-
import android.os.storage.DiskInfo;
-
import android.os.storage.StorageManager;
-
import android.os.storage.StorageVolume;
-
import android.os.storage.VolumeInfo;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.ImageView;
-
import android.widget.LinearLayout;
-
-
public
class UsbBootReceiver extends BroadcastReceiver {
-
private Context mContext;
-
private
static StorageManager mStorageManager =
null;
-
SetVolumeListen setVolumeListen;
-
-
private
final
static Singleton<UsbBootReceiver, Void> mCXBootReceiver =
new Singleton<UsbBootReceiver, Void>() {
-
@Override
-
protected UsbBootReceiver create(Void v) {
-
return
new UsbBootReceiver();
-
}
-
};
-
-
public void registerReceiver(Context mContext) {
-
Log.e(
"TAG",
"registerReceiver");
-
try {
-
IntentFilter mIntentFilter =
new IntentFilter();
-
mIntentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
-
mIntentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
-
mIntentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
-
mIntentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
-
mIntentFilter.addAction(Intent.ACTION_MASTER_CLEAR);
-
-
mIntentFilter.addDataScheme(
"file");
-
mContext.registerReceiver(
this, mIntentFilter);
-
}
catch (Throwable e) {
-
-
}
-
}
-
-
public static String getMANUFACTURER() {
-
return android.os.Build.PRODUCT;
-
}
-
-
public static UsbBootReceiver getInstance() {
-
return mCXBootReceiver.get(
null);
-
}
-
-
@SuppressLint("NewApi")
-
public void onReceive(Context context, Intent intent) {
-
final String action = intent.getAction();
-
mContext = context;
-
// if (getMANUFACTURER().equals("rk312x")) {
-
GetUsbTf(mContext);
-
// }
-
Log.e(
"onReceive",
-
action +
" "
-
+ Intent.ACTION_MEDIA_MOUNTED.equals(action));
-
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
-
startTest(intent.getData().getPath());
-
}
-
}
-
-
private List<Integer> listV =
new ArrayList<Integer>();
-
-
private List<String> listPath =
new ArrayList<String>();
-
-
// 在Rk3128上面 判断是否已经插了TF卡跟Sd卡
-
@SuppressLint("NewApi")
-
public void GetUsbTf(Context context) {
-
listV.clear();
-
listPath.clear();
-
if (mStorageManager ==
null)
-
mStorageManager = (StorageManager) context
-
.getSystemService(StorageManager.class);
-
init_StoragePath(context, mStorageManager);
-
final StorageVolume[] volumes = mStorageManager.getVolumeList();
-
for (StorageVolume vo : volumes) {
-
listPath.add(vo.getPath());
-
// Log.e("TAG",
-
// vo.toString() + " ====== " + vo.getMaxFileSize());
-
if (!vo.isEmulated()) {
-
if (sdcard_dir.equals(vo.getPath())) {
-
// Log.e("TAG", "sd");
-
setVolumeListen.UVolume(
1, vo.getPath());
-
listV.add(
1);
-
-
}
else {
-
// Log.e("TAG", "u盘");
-
setVolumeListen.UVolume(
2, vo.getPath());
-
listV.add(
2);
-
-
}
-
}
else {
-
// Log.e("TAG", "内置");
-
setVolumeListen.UVolume(
3, vo.getPath());
-
listV.add(
3);
-
-
}
-
-
}
-
setVolumeListen.UVloumeList(listV);
-
setVolumeListen.UFileList(listPath);
-
}
-
-
private String sdcard_dir =
"/mnt/external_sd";
-
-
public void init_StoragePath(Context context, StorageManager mStorageManager) {
-
-
final List<VolumeInfo> volumes = mStorageManager.getVolumes();
-
for (VolumeInfo vol : volumes) {
-
if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
-
-
DiskInfo disk = vol.getDisk();
-
if (disk !=
null) {
-
if (disk.isSd()) {
-
// sdcard dir
-
StorageVolume sv = vol.buildStorageVolume(context,
-
context.getUserId(),
false);
-
sdcard_dir = sv.getPath();
-
}
-
}
-
}
-
}
-
}
-
-
public void setUsbIcon(Context context) {
-
mContext = context;
-
}
-
-
private void startTest(String path) {
-
Log.e(
"startTest", path +
"=============================="+(
new File(path +
"/SelectCompany.txt")).exists());
-
-
}
-
-
public
interface SetVolumeListen {
-
// 读取已经有的外部设备 以及外部设备地址
-
void UVolume(int postion, String path);
-
-
// 存储现在已经插在设备上的TF卡 U盘
-
void UVloumeList(List<Integer> list);
-
-
// 获取所有列表
-
void UFileList(List<String> list);
-
-
}
-
-
public void setOnChangeListener(SetVolumeListen setVolumeListen2) {
-
this.setVolumeListen = setVolumeListen2;
-
}
-
}
ReadAssetsUtils
package com.example.utrl; import android.content.Context; import android.os.Environment; import android.text.TextUtils; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class ReadAssetsUtils { /** * 读取apk内部文件 * * @param context * 上下文对象 * @param fileName * 文件名 * @return */ public static String getData(Context context, String fileName) { String result = null; if (TextUtils.isEmpty(fileName)) { return result; } try { InputStream is = context.getAssets().open(fileName); int size = is.available(); byte[] b = new byte[size]; is.read(b); result = new String(b, "UTF-8"); is.close(); } catch (IOException e) { e.printStackTrace(); } return result; } /* * 判断操作 如果文件内有内容则不操作 如果没有的话直接使用另外一个 */ public static String getdefaultData(Context context, String fileName, String Name) { String result = "0"; if (TextUtils.isEmpty(fileName)) { return result; } File file = new File(fileName); if (file.exists()) { try { InputStream is = new FileInputStream( new File(fileName)); int size = is.available(); byte[] b = new byte[size]; is.read(b); result = new String(b, "UTF-8"); is.close(); } catch (IOException e) { e.printStackTrace(); } } else { result = getData(context, Name); return result; } // Log.e("TAG",result); return result; } /** * * @param ctx * @param filePath * 文件地址 * @param filename * 文件名 * @return */ public static boolean CopyFileByAssets(Context ctx, String filePath, String filename) { if (TextUtils.isEmpty(filePath) || TextUtils.isEmpty(filename)) { return false; } File file = new File(filePath + filename); if (file.exists()) { return true; } File applibs = new File(filePath); if (!applibs.exists()) { applibs.mkdir(); } boolean copyIsFinish = false; try { InputStream is = ctx.getAssets().open(filename); // File file = new File(path); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); byte[] temp = new byte[ 1024]; int i = 0; while ((i = is.read(temp)) > 0) { fos.write(temp, 0, i); } fos.close(); is.close(); copyIsFinish = true; file = null; } catch (IOException e) { e.printStackTrace(); } return copyIsFinish; } // 往SD卡写入文件的方法 public static void savaFileToSD(String filename, String filecontent) throws Exception { // 如果手机已插入sd卡,且app具有读写sd卡的权限 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { filename = Environment.getExternalStorageDirectory() .getCanonicalPath() + "/" + filename; // 这里就不要用openFileOutput了,那个是往手机内存中写数据的 FileOutputStream output = new FileOutputStream(filename); output.write(filecontent.getBytes()); output.flush(); // 将String字符串以字节流的形式写入到输出流中 output.close(); // 关闭输出流 } else Log.i( "savaFileToSD", "SD卡不存在或者不可读写"); } // 读取SD卡中文件的方法 // 定义读取文件的方法: public static String readFromSD(String filename, String nulls) throws IOException { StringBuilder sb = new StringBuilder( ""); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { filename = Environment.getExternalStorageDirectory() .getCanonicalPath() + "/" + filename; // 打开文件输入流 FileInputStream input = new FileInputStream(filename); byte[] temp = new byte[ 1024]; int len = 0; // 读取文件内容: while ((len = input.read(temp)) > 0) { sb.append( new String(temp, 0, len)); } // 关闭输入流 input.close(); } String str = sb.toString(); // Log.e("TAG", str + "======read======"); if (str == null || str.trim().equals( "")) { return nulls; } else return str; } // 读取节点内内容 public static int Getnode(String Path) { Log.e( "TAG", "node:" + Path); try { InputStream WorkUpWriter = new FileInputStream(Path); return WorkUpWriter.read(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }Singleton
package com.example.tfsd; public abstract class Singleton<T, P> { private volatile T mInstance; public Singleton() { } protected abstract T create(P var1); public final T get(P p) { if ( this.mInstance == null) { synchronized ( this) { if ( this.mInstance == null) { this.mInstance = this.create(p); } } } return this.mInstance; } }如果你此时把三个内导入到你的项目中,你就会发现,有些方法会报错,比如UsbBootReceiver中的StorageVolume,因为这个是rk的fromwork层的东西,我用的是32885.1的class包,你们如果使用的话,需要你们把定制的rk系统工程师给你们编译一个class包
使用方式
val mUsbBootReceiver = UsbBootReceiver.getInstance() //注册广播 拔插广播 mUsbBootReceiver.registerReceiver( this) mUsbBootReceiver.setOnChangeListener( object : UsbBootReceiver.SetVolumeListen { override fun UVolume(postion: Int, path: String) { } override fun UVloumeList(list: List<Int>) { } override fun UFileList(p0: MutableList<String>?) { } }) mUsbBootReceiver.GetUsbTf( this)使用方法很简单,回调方式都在这!
转载:https://blog.csdn.net/qq_36333309/article/details/111508789
查看评论