I have a file open on an SD card. When someone mounts an SD card, this causes my app to crash. I'm trying to register to broadcast ACTION_MEDIA_EJECT events, and I get this, but it looks like I'm too late. By the time I realized this, he had already crashed my application. Is there a way to get notified before my application crashes?
A very simple code example has been added. When I do this, it crashes the service when I turn on USB (MSC mode).
Test.java
@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; } }
source share