android通过代码来实现卸载SD卡(转载)
在一些时候,需要卸载SD卡来测试东西,但眼前的没有合适的手机让你拔掉SD卡,那么可以通过以下代买来卸载SD卡。
代码是
private void doUnmount() { IMountService mountService = getMountService(); String extStoragePath = Environment.getExternalStorageDirectory().toString(); try { mountService.unmountVolume(extStoragePath, true, true); } catch (RemoteException e) { Log.i("TT","unmount failed."); } } private synchronized IMountService getMountService() { IMountService mMountService = null; if (mMountService == null) { IBinder service = ServiceManager.getService("mount"); if (service != null) { mMountService = IMountService.Stub.asInterface(service); } else { Log.e("TT", "Can't get mount service"); } } return mMountService; }
但这会发现在android4.1中没有相关的接口。所以必须导入framework_intermediates下面的classes jar包就OK了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步