public void onCreate() { Runnable downloadChecker = new Runnable() { @Override public void run() { try { downloadService.checkDownloads(); } catch (Throwable x) { Log.e(TAG, "checkDownloads() failed.", x); } } }; executorService = Executors.newScheduledThreadPool(2); executorService.scheduleWithFixedDelay(downloadChecker, 5, 5, TimeUnit.SECONDS); // Pause when headset is unplugged. headsetEventReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "Headset event for: " + intent.getExtras().get("name")); if (intent.getExtras().getInt("state") == 0) { downloadService.pause(); } } }; downloadService.registerReceiver( headsetEventReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); // 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); // Register the handler for outside intents. IntentFilter commandFilter = new IntentFilter(); commandFilter.addAction(DownloadServiceImpl.CMD_PLAY); commandFilter.addAction(DownloadServiceImpl.CMD_TOGGLEPAUSE); commandFilter.addAction(DownloadServiceImpl.CMD_PAUSE); commandFilter.addAction(DownloadServiceImpl.CMD_STOP); commandFilter.addAction(DownloadServiceImpl.CMD_PREVIOUS); commandFilter.addAction(DownloadServiceImpl.CMD_NEXT); downloadService.registerReceiver(intentReceiver, commandFilter); deserializeDownloadQueue(); new CacheCleaner(downloadService, downloadService).clean(); }