硬件平台:展讯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 - SAX解析XML数据
  2. Android Touch事件传递机制具体解释 上
  3. Android客户端向服务器端发送数据的流程(1)
  4. android 事件处理(三)
  5. Android的数据存储之一------SharedPreferences

随机推荐

  1. android设备上视频只有声音没有图像
  2. 2011.06.23——— android 事件处理机制
  3. Android(安卓)SD
  4. Android(安卓)JNI 开启子线程后调用 Acti
  5. Android(安卓)dialog 全屏
  6. android中判断网络是否连接
  7. Android(安卓)sdk update
  8. Android关于自定义ExpandableListView样
  9. android 学习记录备忘录
  10. android中判断网络是否连接