在接收到ACTION_MEDIA_EJECT或者ACTION_MEDIA_UNMOUNTED广播后,可以通过以下步骤使U盘成功卸载,不会再下次插入的时候系统提示“vold : ForceUnmount failed: Device or resource busy”: 停止对U盘中工厂apk的读写操作,即关闭工厂apk应用程序。 关闭U盘中工厂apk应用程序所打开的所有文件和流。 关闭U盘中工厂apk应用程序所占用的所有资源。 等待一段时间,确保U盘中工厂apk应用程序已经完全释放了对U盘的占用。 卸载U盘。 另外,为了避免下次插入时提示“vold : ForceUnmount failed: Device or resource busy”,可以在卸载U盘后,检查U盘是否已经完全卸载,如果没有完全卸载,可以尝试强制卸载U盘。如果还是不行,可以重启设备再尝试卸载。 请详细写出代码实现方式
时间: 2024-02-18 12:59:08 浏览: 144
CD_Driver.zip_CD PLAY_cd_eject_play
以下是示例代码实现方式:
```
public class MainActivity extends AppCompatActivity {
private static final String FACTORY_APK_NAME = "factory.apk";
private static final String FACTORY_APK_PACKAGE_NAME = "com.example.factory";
private static final int WAIT_TIME = 5000; // 等待时间,单位为毫秒
private boolean mIsFactoryApkRunning = false;
private boolean mIsUninstalling = false;
private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case Intent.ACTION_MEDIA_MOUNTED:
// 获取U盘中的工厂apk信息
String apkPath = getFactoryApkPath(context);
if (apkPath != null) {
// 启动工厂apk
startFactoryApk(context, apkPath);
}
break;
case Intent.ACTION_MEDIA_EJECT:
case Intent.ACTION_MEDIA_UNMOUNTED:
// 停止工厂apk
stopFactoryApk(context);
// 卸载U盘
uninstallUsb(context);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注册U盘相关广播接收器
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
registerReceiver(mUsbReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 注销U盘相关广播接收器
unregisterReceiver(mUsbReceiver);
}
private String getFactoryApkPath(Context context) {
String apkPath = null;
File usbRoot = getUsbRoot(context);
if (usbRoot != null) {
File[] files = usbRoot.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.equals(FACTORY_APK_NAME);
}
});
if (files != null && files.length > 0) {
apkPath = files[0].getAbsolutePath();
}
}
return apkPath;
}
private File getUsbRoot(Context context) {
File usbRoot = null;
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
String[] volumePaths = (String[]) storageManager.getClass().getMethod("getVolumePaths").invoke(storageManager);
for (String path : volumePaths) {
File file = new File(path);
if (file.exists() && file.isDirectory() && file.canRead()) {
StatFs statFs = new StatFs(file.getAbsolutePath());
if (statFs.getBlockCountLong() > 0 && statFs.getBlockSizeLong() > 0 &&
statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong() > 0) {
usbRoot = file;
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return usbRoot;
}
private void startFactoryApk(Context context, String apkPath) {
if (mIsFactoryApkRunning) {
return;
}
Intent intent = context.getPackageManager().getLaunchIntentForPackage(FACTORY_APK_PACKAGE_NAME);
if (intent == null) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
mIsFactoryApkRunning = true;
}
private void stopFactoryApk(Context context) {
if (!mIsFactoryApkRunning) {
return;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfoList = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
if (processInfo.processName.equals(FACTORY_APK_PACKAGE_NAME)) {
android.os.Process.killProcess(processInfo.pid);
break;
}
}
mIsFactoryApkRunning = false;
}
private void uninstallUsb(final Context context) {
if (mIsUninstalling) {
return;
}
mIsUninstalling = true;
new Thread(new Runnable() {
@Override
public void run() {
while (isFactoryApkRunning(context)) {
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String path = getUsbRoot(context).getAbsolutePath();
String cmd = "pm uninstall " + path;
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
mIsUninstalling = false;
}
}).start();
}
private boolean isFactoryApkRunning(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfoList = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
if (processInfo.processName.equals(FACTORY_APK_PACKAGE_NAME)) {
return true;
}
}
return false;
}
}
```
以上是一个简单的示例代码实现方式,具体实现方式可能会因为需求和环境的不同而有所不同。
阅读全文