下面列出了android.content.Intent#ACTION_MEDIA_EJECT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void installIntentFilter()
{
// install an intent filter to receive SD card related events.
IntentFilter intentFilter =
new IntentFilter(Intent.ACTION_MEDIA_EJECT);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addDataScheme("file");
mReceiver = new MyBroadcastReceiver();
mActivity.registerReceiver(mReceiver, intentFilter);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == Intent.ACTION_MEDIA_EJECT) {
//do nothings
} else if (intent.getAction() == Intent.ACTION_MEDIA_SHARED) {
//do nothings
} else if (intent.getAction() == Intent.ACTION_MEDIA_BAD_REMOVAL) {
//do nothings
} else if (intent.getAction() == Intent.ACTION_MEDIA_REMOVED) {
notifyStorageRemove(context);
} else if (intent.getAction() == Intent.ACTION_MEDIA_MOUNTED) {
notifyStorageMount(context);
}
}
public void onCreate() {
new Thread(() -> {
Looper.prepare();
eventLooper = Looper.myLooper();
eventHandler = new Handler(eventLooper);
// Deserialize queue before starting looper
try {
lock.lock();
deserializeDownloadQueueNow();
// Wait until PREPARING is done to mark lifecycle as ready to receive events
while (downloadService.getPlayerState() == PREPARING) {
Util.sleepQuietly(50L);
}
setup.set(true);
} finally {
lock.unlock();
}
Looper.loop();
}, "DownloadServiceLifecycleSupport").start();
// Stop when SD card is ejected.
ejectEventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
externalStorageAvailable = Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction());
if (!externalStorageAvailable) {
Log.i(TAG, "External media is ejecting. Stopping playback.");
downloadService.reset();
} else {
Log.i(TAG, "External media is available.");
}
}
};
IntentFilter ejectFilter = new IntentFilter(Intent.ACTION_MEDIA_EJECT);
ejectFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
ejectFilter.addDataScheme("file");
downloadService.registerReceiver(ejectEventReceiver, ejectFilter);
// React to media buttons.
Util.registerMediaButtonEventReceiver(downloadService);
// Pause temporarily on incoming phone calls.
phoneStateListener = new MyPhoneStateListener();
// Android 6.0 removes requirement for android.Manifest.permission.READ_PHONE_STATE;
TelephonyManager telephonyManager = (TelephonyManager) downloadService.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
// Register the handler for outside intents.
IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(DownloadService.CMD_PLAY);
commandFilter.addAction(DownloadService.CMD_TOGGLEPAUSE);
commandFilter.addAction(DownloadService.CMD_PAUSE);
commandFilter.addAction(DownloadService.CMD_STOP);
commandFilter.addAction(DownloadService.CMD_PREVIOUS);
commandFilter.addAction(DownloadService.CMD_NEXT);
commandFilter.addAction(DownloadService.CANCEL_DOWNLOADS);
downloadService.registerReceiver(intentReceiver, commandFilter);
new CacheCleaner(downloadService, downloadService).clean();
}
public void onCreate() {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
eventLooper = Looper.myLooper();
eventHandler = new Handler(eventLooper);
// Deserialize queue before starting looper
try {
lock.lock();
deserializeDownloadQueueNow();
// Wait until PREPARING is done to mark lifecycle as ready to receive events
while(downloadService.getPlayerState() == PREPARING) {
Util.sleepQuietly(50L);
}
setup.set(true);
} finally {
lock.unlock();
}
Looper.loop();
}
}, "DownloadServiceLifecycleSupport").start();
// Stop when SD card is ejected.
ejectEventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
externalStorageAvailable = Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction());
if (!externalStorageAvailable) {
Log.i(TAG, "External media is ejecting. Stopping playback.");
downloadService.reset();
} else {
Log.i(TAG, "External media is available.");
}
}
};
IntentFilter ejectFilter = new IntentFilter(Intent.ACTION_MEDIA_EJECT);
ejectFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
ejectFilter.addDataScheme("file");
downloadService.registerReceiver(ejectEventReceiver, ejectFilter);
// React to media buttons.
Util.registerMediaButtonEventReceiver(downloadService);
// Pause temporarily on incoming phone calls.
phoneStateListener = new MyPhoneStateListener();
// Android 6.0 removes requirement for android.Manifest.permission.READ_PHONE_STATE;
TelephonyManager telephonyManager = (TelephonyManager) downloadService.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
// Register the handler for outside intents.
IntentFilter commandFilter = new IntentFilter();
commandFilter.addAction(DownloadService.CMD_PLAY);
commandFilter.addAction(DownloadService.CMD_TOGGLEPAUSE);
commandFilter.addAction(DownloadService.CMD_PAUSE);
commandFilter.addAction(DownloadService.CMD_STOP);
commandFilter.addAction(DownloadService.CMD_PREVIOUS);
commandFilter.addAction(DownloadService.CMD_NEXT);
commandFilter.addAction(DownloadService.CANCEL_DOWNLOADS);
downloadService.registerReceiver(intentReceiver, commandFilter);
new CacheCleaner(downloadService, downloadService).clean();
}