我该如何安装SD卡程序?

时间:2010-09-15 08:56:09

标签: android

我想安装sdcard programmatic,我该怎么检查?

2 个答案:

答案 0 :(得分:1)

如果您正在编写普通的SDK应用程序,则无法自行安装SD卡。

如果您为设备制造商工作,或者您正在构建可以使用固件签名密钥签名的应用程序,则可以使用USB_MASS_STORAGE_ENABLED

答案 1 :(得分:1)

为了使其有效,您需要添加用户库classes-full-debug.jar(来自aosp或cm build) BEFORE android.jar(构建路径中有一个面板)要对罐子进行排序)或StorageManager无法解决registerListener() 您还需要android.permission.MOUNT_UNMOUNT_FILESYSTEMS

package x.y.z;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.storage.IMountService;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.ServiceManager;
import android.widget.TextView;

public class MyActivity extends Activity
{
    private static final String MOUNTPOINT = "/mnt/sdcard";
    private IMountService mMountService;
    private StorageManager mStorageManager;
    private TextView mText;

    private final StorageEventListener mStorageListener = new StorageEventListener()
    {
        @Override
        public void onStorageStateChanged(String path, String oldState, String newState)
        {
            String text = mText.getText() + "\n";
            text += "state changed notification that " + path + " changed state from " + oldState + " to " + newState;
            mText.setText(text);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mText = (TextView) findViewById(R.id.textView);

        if (mMountService == null)
        {
            IBinder service = ServiceManager.getService("mount");
            mMountService = IMountService.Stub.asInterface(service);
        }

        if (mStorageManager == null)
        {
            mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
            mStorageManager.registerListener(mStorageListener);        
        }

        try
        {
            String state = mMountService.getVolumeState(MOUNTPOINT);
            mText.setText("Media state " + state);
            if (state.equals(Environment.MEDIA_MOUNTED))
                mMountService.unmountVolume(MOUNTPOINT, false);
            else if (state.equals(Environment.MEDIA_UNMOUNTED))
                mMountService.mountVolume(MOUNTPOINT);
        } catch (RemoteException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy()
    {
        if (mStorageManager != null && mStorageListener != null)
            mStorageManager.unregisterListener(mStorageListener);
        super.onDestroy();
    }
}