硬件平台:展讯8541e

软件平台:Andoir8.1

需求:智能硬件连接PC之后,走MTP模式,数据不同步,重新插拔USB线也无作用,只有重启才能同步数据,因此需要添加USB事件触发同步事件。

diff --git a/src/com/android/providers/media/MtpReceiver.java b/src/com/android/providers/media/MtpReceiver.java
index ae5b490..88ded32 100755
--- a/src/com/android/providers/media/MtpReceiver.java
+++ b/src/com/android/providers/media/MtpReceiver.java
@@ -27,6 +27,8 @@ import android.os.Bundle;
 import android.os.UserHandle;
 import android.util.Log;
 import android.mtp.MtpServer;
+import android.os.Environment;
+import android.os.EnvironmentEx;

 
 public class MtpReceiver extends BroadcastReceiver {
     private static final String TAG = MtpReceiver.class.getSimpleName();
@@ -72,6 +74,12 @@ public class MtpReceiver extends BroadcastReceiver {
             }
             if (DEBUG) { Log.d(TAG, "handleUsbState startService"); }
             context.startService(intent);
+            // kbding add for mtp scanner
+            scan(context, MediaProvider.INTERNAL_VOLUME, null);
+            // scan the inner storage
+            String internalStoragePath = EnvironmentEx.getInternalStoragePath().getPath();
+            scan(context, MediaProvider.EXTERNAL_VOLUME, internalStoragePath);
+            // kbding add end

         } else if (!connected || !(mtpEnabled || ptpEnabled)) {
             // Only unbind if disconnected or disabled.
             hasConfigured = false;
@@ -92,4 +100,15 @@ public class MtpReceiver extends BroadcastReceiver {
                     "content://media/none/mtp_connected"), null);
         }
     }
+
+    private void scan(Context context, String volume, String path) {
+        Log.d(TAG, "scan " + volume + " volume path: " + (null == path
+                ? (Environment.getRootDirectory() + "/media") : path));
+        Bundle args = new Bundle();
+        args.putString("volume", volume);
+        // SPRD: Add
+        args.putString("path", path);
+        context.startService(
+                new Intent(context, MediaScannerService.class).putExtras(args));
+    }

 }

实现的思路是在USB插拔事件触发之后,会重新扫描相应的存储目录,同步数据库。

 

Mark一下~

 

 

 

 

 

 

 

 

 

 

更多相关文章

  1. Android(安卓)中 Button 的基本使用
  2. Android(安卓)Touch事件传递机制具体解释 上
  3. Android(安卓)NestedScrolling机制完全解析 带你玩转嵌套滑动
  4. android触控,先了解MotionEvent(一)
  5. android 事件处理(三)
  6. Android事件处理第一节(View对Touch事件的处理)
  7. Android中线程同步之Mutex与Condtion的用法
  8. 【Android】ListView与Button的共存问题解决
  9. View事件分发机制

随机推荐

  1. Android启动画面实现
  2. android 删除的警告对话框
  3. android usb host 读写USB设备
  4. Android下监听Home键
  5. 修改Android EditText光标颜色
  6. android九宫格
  7. :Android Market的 Loading效果
  8. Android开发入门_环境搭建
  9. android中 MediaStore提取缩略图和原始图
  10. 安卓巴士Android开发神贴整理