飞道的博客

Rk安卓主板app开发之实时监听外部设备插入以及地址获取

236人阅读  评论(0)

前言:在rk的主板上,因为是定制的东西,所以有些东西跟原生的有些不一样,比如监听外部设备的拔插以及地址获取

不废话了直接上代码:下面的是一个广播 


  
  1. package com.example.receiver;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import com.example.tfsd.Singleton;
  6. import com.example.utrl.ReadAssetsUtils;
  7. import android.annotation.SuppressLint;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.IntentFilter;
  12. import android.os.storage.DiskInfo;
  13. import android.os.storage.StorageManager;
  14. import android.os.storage.StorageVolume;
  15. import android.os.storage.VolumeInfo;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.ImageView;
  19. import android.widget.LinearLayout;
  20. public class UsbBootReceiver extends BroadcastReceiver {
  21. private Context mContext;
  22. private static StorageManager mStorageManager = null;
  23. SetVolumeListen setVolumeListen;
  24. private final static Singleton<UsbBootReceiver, Void> mCXBootReceiver = new Singleton<UsbBootReceiver, Void>() {
  25. @Override
  26. protected UsbBootReceiver create(Void v) {
  27. return new UsbBootReceiver();
  28. }
  29. };
  30. public void registerReceiver(Context mContext) {
  31. Log.e( "TAG", "registerReceiver");
  32. try {
  33. IntentFilter mIntentFilter = new IntentFilter();
  34. mIntentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
  35. mIntentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
  36. mIntentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
  37. mIntentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
  38. mIntentFilter.addAction(Intent.ACTION_MASTER_CLEAR);
  39. mIntentFilter.addDataScheme( "file");
  40. mContext.registerReceiver( this, mIntentFilter);
  41. } catch (Throwable e) {
  42. }
  43. }
  44. public static String getMANUFACTURER() {
  45. return android.os.Build.PRODUCT;
  46. }
  47. public static UsbBootReceiver getInstance() {
  48. return mCXBootReceiver.get( null);
  49. }
  50. @SuppressLint("NewApi")
  51. public void onReceive(Context context, Intent intent) {
  52. final String action = intent.getAction();
  53. mContext = context;
  54. // if (getMANUFACTURER().equals("rk312x")) {
  55. GetUsbTf(mContext);
  56. // }
  57. Log.e( "onReceive",
  58. action + " "
  59. + Intent.ACTION_MEDIA_MOUNTED.equals(action));
  60. if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
  61. startTest(intent.getData().getPath());
  62. }
  63. }
  64. private List<Integer> listV = new ArrayList<Integer>();
  65. private List<String> listPath = new ArrayList<String>();
  66. // 在Rk3128上面 判断是否已经插了TF卡跟Sd卡
  67. @SuppressLint("NewApi")
  68. public void GetUsbTf(Context context) {
  69. listV.clear();
  70. listPath.clear();
  71. if (mStorageManager == null)
  72. mStorageManager = (StorageManager) context
  73. .getSystemService(StorageManager.class);
  74. init_StoragePath(context, mStorageManager);
  75. final StorageVolume[] volumes = mStorageManager.getVolumeList();
  76. for (StorageVolume vo : volumes) {
  77. listPath.add(vo.getPath());
  78. // Log.e("TAG",
  79. // vo.toString() + " ====== " + vo.getMaxFileSize());
  80. if (!vo.isEmulated()) {
  81. if (sdcard_dir.equals(vo.getPath())) {
  82. // Log.e("TAG", "sd");
  83. setVolumeListen.UVolume( 1, vo.getPath());
  84. listV.add( 1);
  85. } else {
  86. // Log.e("TAG", "u盘");
  87. setVolumeListen.UVolume( 2, vo.getPath());
  88. listV.add( 2);
  89. }
  90. } else {
  91. // Log.e("TAG", "内置");
  92. setVolumeListen.UVolume( 3, vo.getPath());
  93. listV.add( 3);
  94. }
  95. }
  96. setVolumeListen.UVloumeList(listV);
  97. setVolumeListen.UFileList(listPath);
  98. }
  99. private String sdcard_dir = "/mnt/external_sd";
  100. public void init_StoragePath(Context context, StorageManager mStorageManager) {
  101. final List<VolumeInfo> volumes = mStorageManager.getVolumes();
  102. for (VolumeInfo vol : volumes) {
  103. if (vol.getType() == VolumeInfo.TYPE_PUBLIC) {
  104. DiskInfo disk = vol.getDisk();
  105. if (disk != null) {
  106. if (disk.isSd()) {
  107. // sdcard dir
  108. StorageVolume sv = vol.buildStorageVolume(context,
  109. context.getUserId(), false);
  110. sdcard_dir = sv.getPath();
  111. }
  112. }
  113. }
  114. }
  115. }
  116. public void setUsbIcon(Context context) {
  117. mContext = context;
  118. }
  119. private void startTest(String path) {
  120. Log.e( "startTest", path + "=============================="+( new File(path + "/SelectCompany.txt")).exists());
  121. }
  122. public interface SetVolumeListen {
  123. // 读取已经有的外部设备 以及外部设备地址
  124. void UVolume(int postion, String path);
  125. // 存储现在已经插在设备上的TF卡 U盘
  126. void UVloumeList(List<Integer> list);
  127. // 获取所有列表
  128. void UFileList(List<String> list);
  129. }
  130. public void setOnChangeListener(SetVolumeListen setVolumeListen2) {
  131. this.setVolumeListen = setVolumeListen2;
  132. }
  133. }

 ReadAssetsUtils


   
  1. package com.example.utrl;
  2. import android.content.Context;
  3. import android.os.Environment;
  4. import android.text.TextUtils;
  5. import android.util.Log;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. public class ReadAssetsUtils {
  13. /**
  14. * 读取apk内部文件
  15. *
  16. * @param context
  17. * 上下文对象
  18. * @param fileName
  19. * 文件名
  20. * @return
  21. */
  22. public static String getData(Context context, String fileName) {
  23. String result = null;
  24. if (TextUtils.isEmpty(fileName)) {
  25. return result;
  26. }
  27. try {
  28. InputStream is = context.getAssets().open(fileName);
  29. int size = is.available();
  30. byte[] b = new byte[size];
  31. is.read(b);
  32. result = new String(b, "UTF-8");
  33. is.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. return result;
  38. }
  39. /*
  40. * 判断操作 如果文件内有内容则不操作 如果没有的话直接使用另外一个
  41. */
  42. public static String getdefaultData(Context context, String fileName,
  43. String Name) {
  44. String result = "0";
  45. if (TextUtils.isEmpty(fileName)) {
  46. return result;
  47. }
  48. File file = new File(fileName);
  49. if (file.exists()) {
  50. try {
  51. InputStream is = new FileInputStream( new File(fileName));
  52. int size = is.available();
  53. byte[] b = new byte[size];
  54. is.read(b);
  55. result = new String(b, "UTF-8");
  56. is.close();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. } else {
  61. result = getData(context, Name);
  62. return result;
  63. }
  64. // Log.e("TAG",result);
  65. return result;
  66. }
  67. /**
  68. *
  69. * @param ctx
  70. * @param filePath
  71. * 文件地址
  72. * @param filename
  73. * 文件名
  74. * @return
  75. */
  76. public static boolean CopyFileByAssets(Context ctx, String filePath,
  77. String filename) {
  78. if (TextUtils.isEmpty(filePath) || TextUtils.isEmpty(filename)) {
  79. return false;
  80. }
  81. File file = new File(filePath + filename);
  82. if (file.exists()) {
  83. return true;
  84. }
  85. File applibs = new File(filePath);
  86. if (!applibs.exists()) {
  87. applibs.mkdir();
  88. }
  89. boolean copyIsFinish = false;
  90. try {
  91. InputStream is = ctx.getAssets().open(filename);
  92. // File file = new File(path);
  93. file.createNewFile();
  94. FileOutputStream fos = new FileOutputStream(file);
  95. byte[] temp = new byte[ 1024];
  96. int i = 0;
  97. while ((i = is.read(temp)) > 0) {
  98. fos.write(temp, 0, i);
  99. }
  100. fos.close();
  101. is.close();
  102. copyIsFinish = true;
  103. file = null;
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. return copyIsFinish;
  108. }
  109. // 往SD卡写入文件的方法
  110. public static void savaFileToSD(String filename, String filecontent)
  111. throws Exception {
  112. // 如果手机已插入sd卡,且app具有读写sd卡的权限
  113. if (Environment.getExternalStorageState().equals(
  114. Environment.MEDIA_MOUNTED)) {
  115. filename = Environment.getExternalStorageDirectory()
  116. .getCanonicalPath() + "/" + filename;
  117. // 这里就不要用openFileOutput了,那个是往手机内存中写数据的
  118. FileOutputStream output = new FileOutputStream(filename);
  119. output.write(filecontent.getBytes());
  120. output.flush();
  121. // 将String字符串以字节流的形式写入到输出流中
  122. output.close();
  123. // 关闭输出流
  124. } else
  125. Log.i( "savaFileToSD", "SD卡不存在或者不可读写");
  126. }
  127. // 读取SD卡中文件的方法
  128. // 定义读取文件的方法:
  129. public static String readFromSD(String filename, String nulls)
  130. throws IOException {
  131. StringBuilder sb = new StringBuilder( "");
  132. if (Environment.getExternalStorageState().equals(
  133. Environment.MEDIA_MOUNTED)) {
  134. filename = Environment.getExternalStorageDirectory()
  135. .getCanonicalPath() + "/" + filename;
  136. // 打开文件输入流
  137. FileInputStream input = new FileInputStream(filename);
  138. byte[] temp = new byte[ 1024];
  139. int len = 0;
  140. // 读取文件内容:
  141. while ((len = input.read(temp)) > 0) {
  142. sb.append( new String(temp, 0, len));
  143. }
  144. // 关闭输入流
  145. input.close();
  146. }
  147. String str = sb.toString();
  148. // Log.e("TAG", str + "======read======");
  149. if (str == null || str.trim().equals( "")) {
  150. return nulls;
  151. } else
  152. return str;
  153. }
  154. // 读取节点内内容
  155. public static int Getnode(String Path) {
  156. Log.e( "TAG", "node:" + Path);
  157. try {
  158. InputStream WorkUpWriter = new FileInputStream(Path);
  159. return WorkUpWriter.read();
  160. } catch (Exception e) {
  161. // TODO Auto-generated catch block
  162. e.printStackTrace();
  163. }
  164. return 0;
  165. }
  166. }

Singleton


   
  1. package com.example.tfsd;
  2. public abstract class Singleton<T, P> {
  3. private volatile T mInstance;
  4. public Singleton() {
  5. }
  6. protected abstract T create(P var1);
  7. public final T get(P p) {
  8. if ( this.mInstance == null) {
  9. synchronized ( this) {
  10. if ( this.mInstance == null) {
  11. this.mInstance = this.create(p);
  12. }
  13. }
  14. }
  15. return this.mInstance;
  16. }
  17. }

如果你此时把三个内导入到你的项目中,你就会发现,有些方法会报错,比如UsbBootReceiver中的StorageVolume,因为这个是rk的fromwork层的东西,我用的是32885.1的class包,你们如果使用的话,需要你们把定制的rk系统工程师给你们编译一个class包

使用方式 


   
  1. val mUsbBootReceiver = UsbBootReceiver.getInstance()
  2. //注册广播 拔插广播
  3. mUsbBootReceiver.registerReceiver( this)
  4. mUsbBootReceiver.setOnChangeListener( object : UsbBootReceiver.SetVolumeListen {
  5. override fun UVolume(postion: Int, path: String) {
  6. }
  7. override fun UVloumeList(list: List<Int>) {
  8. }
  9. override fun UFileList(p0: MutableList<String>?) {
  10. }
  11. })
  12. mUsbBootReceiver.GetUsbTf( this)

使用方法很简单,回调方式都在这!


转载:https://blog.csdn.net/qq_36333309/article/details/111508789
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场