防止有人安装 Android SD 卡时发生崩溃
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它正在终止您的应用程序,因为这是 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.
在不知道更多关于抛出什么异常的情况下,这就是我能推荐的全部内容。
另外,环境类文档中的以下代码可能会有所帮助:
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: