Android8.1添加MTP数据同步操作
硬件平台:展讯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一下~
更多相关文章
- android - SAX解析XML数据
- Android Touch事件传递机制具体解释 上
- Android客户端向服务器端发送数据的流程(1)
- android 事件处理(三)
- Android的数据存储之一------SharedPreferences