preventing崩溃时,有人登上一个Android的SD卡 [英] preventing a crash when someone mounts an Android SD card

查看:104
本文介绍了preventing崩溃时,有人登上一个Android的SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SD卡上打开的文件。当有人挂载SD卡,它缠崩溃我的应用程序。我想为ACTION_MEDIA_EJECT广播事件注册,和我收到,但好像我变得太迟到了。到时候我得到的,它已经坠毁我的申请。有什么办法我崩溃之前的应用程序得到通知?

增加了一些非常简单的示例code。当我做到这一点,缠崩溃的服务时,我打开USB(MSC模式)。

Test.java

  / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    尝试{
        startService(新意图(这一点,TestService.class));
        bindService(新意图(这一点,TestService.class),serviceConnection,Context.BIND_AUTO_CREATE);
    }赶上(例外五){    }
}保护TestService的TestService的;
私人ServiceConnection serviceConnection =新ServiceConnection(){    @覆盖
    公共无效onServiceConnected(组件名名称,服务的IBinder){
        TestService.LocalBinder粘结剂=(TestService.LocalBinder)服务;
        TestService的= binder.getService();
    }    @覆盖
    公共无效onServiceDisconnected(组件名名){
        TestService的= NULL;
    }};

TestService.java

  @覆盖
公共无效的onCreate(){
    super.onCreate();
    尝试{
        mFileOutputStream =新的FileOutputStream(/ SD卡/ test2的,真正的);
    }赶上(例外五){
    }
}@覆盖
公众的IBinder onBind(意向意图){返回粘结剂; }公共静态类LocalBinder扩展粘结剂{
    公共静态TestService的的getService(){
        返回_this;
    }
}


解决方案

据杀死你的应用程序,因为它采用的是Android的书面设计决定杀死任何进程拿着一个打开的句柄到时,SD卡需要从卸载的SD卡的装置(例如是USB安装到连接的PC)。

仍然有打开的文件句柄的文件系统不能彻底卸装。在操作系统级别,没有办法撤销文件句柄,短查杀过程中它得到了批准,所以这就是Android的最终做。

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;
    }
}

解决方案

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.

这篇关于preventing崩溃时,有人登上一个Android的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆