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了。

posted @ 2012-09-26 10:49  chlips  阅读(1092)  评论(0编辑  收藏  举报