防止有人安装 Android SD 卡时发生崩溃

发布于 2024-10-17 19:26:11 字数 1488 浏览 7 评论 0原文

我在 SD 卡上打开了一个文件。当有人安装 SD 卡时,它最终会导致我的应用程序崩溃。我正在尝试注册 ACTION_MEDIA_EJECT 广播事件,并且我收到了该事件,但似乎我来得太晚了。当我得到它时,它已经使我的应用程序崩溃了。有什么方法可以在我的应用程序崩溃之前收到通知吗?

添加了一些非常简单的示例代码。当我这样做时,当我打开 USB(MSC 模式)时,服务最终会崩溃。

测试.java

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        startService(new Intent(this, TestService.class));
        bindService(new Intent(this, TestService.class), serviceConnection, Context.BIND_AUTO_CREATE);
    } catch (Exception e) {

    }
}

protected TestService testService;
private ServiceConnection serviceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        TestService.LocalBinder binder = (TestService.LocalBinder) service;
        testService = binder.getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        testService = null;
    }

};

测试服务.java

@Override
public void onCreate() {
    super.onCreate();
    try {
        mFileOutputStream = new FileOutputStream("/sdcard/test2", true);
    } catch (Exception e) {
    }
}

@Override
public IBinder onBind(Intent intent) { return binder; }

public static class LocalBinder extends Binder {
    public static TestService getService() {
        return _this;
    }
}

I have a file open on the SD card. When someone mounts the SD card, it winds up crashing my application. I am trying to register for the ACTION_MEDIA_EJECT broadcast event, and i receive that, but it seems like i'm getting that too late. By the time I get that, it's already crashed my application. Is there any way to get notified before crashing my application?

Added some very simple sample code. When I do this, it winds up crashing the service when I turn on USB (MSC mode).

Test.java

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        startService(new Intent(this, TestService.class));
        bindService(new Intent(this, TestService.class), serviceConnection, Context.BIND_AUTO_CREATE);
    } catch (Exception e) {

    }
}

protected TestService testService;
private ServiceConnection serviceConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        TestService.LocalBinder binder = (TestService.LocalBinder) service;
        testService = binder.getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        testService = null;
    }

};

TestService.java

@Override
public void onCreate() {
    super.onCreate();
    try {
        mFileOutputStream = new FileOutputStream("/sdcard/test2", true);
    } catch (Exception e) {
    }
}

@Override
public IBinder onBind(Intent intent) { return binder; }

public static class LocalBinder extends Binder {
    public static TestService getService() {
        return _this;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-10-24 19:26:11

它正在终止您的应用程序,因为这是 android 中记录的设计决策,当需要从设备卸载 sdcard 时(例如,通过 USB 安装到连接的 PC),会终止持有 sdcard 打开句柄的任何进程。

仍具有打开文件句柄的文件系统无法完全卸载。在操作系统级别,除了杀死被授予文件句柄的进程之外,没有办法撤销文件句柄,所以这就是 android 最终所做的。

It is killing your application because it is a documented design decision in android to kill any process holding an open handle to the sdcard when the sdcard needs to be unmounted from the device (for example to be USB mounted to a connected PC).

A file system which still has open file handles cannot be cleanly unmounted. At the operating system level, there's no way to revoke a file handle, short of killing the process to which it was granted, so that is what android ultimately does.

所有深爱都是秘密 2024-10-24 19:26:11
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    //access external file
}

在不知道更多关于抛出什么异常的情况下,这就是我能推荐的全部内容。

另外,环境类文档中的以下代码可能会有所帮助:

BroadcastReceiver mExternalStorageReceiver;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

void updateExternalStorageState() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    handleExternalStorageState(mExternalStorageAvailable,
            mExternalStorageWriteable);
}

void startWatchingExternalStorage() {
    mExternalStorageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("test", "Storage: " + intent.getData());
            updateExternalStorageState();
        }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    filter.addAction(Intent.ACTION_MEDIA_REMOVED);
    registerReceiver(mExternalStorageReceiver, filter);
    updateExternalStorageState();
}

void stopWatchingExternalStorage() {
    unregisterReceiver(mExternalStorageReceiver);
}
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    //access external file
}

Without knowing more about what exceptions are being thrown, this is about all I can recommend.

Also, the following code from the Environment class documentation may help:

BroadcastReceiver mExternalStorageReceiver;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;

void updateExternalStorageState() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    handleExternalStorageState(mExternalStorageAvailable,
            mExternalStorageWriteable);
}

void startWatchingExternalStorage() {
    mExternalStorageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("test", "Storage: " + intent.getData());
            updateExternalStorageState();
        }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
    filter.addAction(Intent.ACTION_MEDIA_REMOVED);
    registerReceiver(mExternalStorageReceiver, filter);
    updateExternalStorageState();
}

void stopWatchingExternalStorage() {
    unregisterReceiver(mExternalStorageReceiver);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文