Cómo detener o pausar Pandora y Spotify
Tengo una aplicación que tiene una función para iniciar una aplicación, una estación de Pandora o un acceso directo. Que todo funciona bien. Más tarde quiero parar la aplicación que empecé. Esto funciona para la mayoría de las cosas, excepto que Pandora y Spotify no siempre se cierran. A veces lo hacen pero no siempre. Parece estar relacionado con el estado actual de la interfaz de usuario. Por ejemplo, funciona bien cuando se muestra Pandora o se muestra la pantalla de inicio. Cuando Home Dock o Car Mode está activo, no funciona. Puedes ver todo mi código fuente aquí:http://code.google.com/p/a2dpvolume/ service.java es el archivo que tiene esta funcionalidad.
Aquí está la parte de ese código que intenta detener la reproducción de la música y luego detener la aplicación.
<code>if (bt2.hasIntent()) { // if music is playing, pause it if (am2.isMusicActive()) { // first pause the music so it removes the notify icon Intent i = new Intent("com.android.music.musicservicecommand"); i.putExtra("command", "pause"); sendBroadcast(i); // for more stubborn players, try this too... Intent downIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent2 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP); downIntent2.putExtra(Intent.EXTRA_KEY_EVENT, downEvent2); sendOrderedBroadcast(downIntent2, null); } // if we opened a package for this device, try to close it now if (bt2.getPname().length() > 3 && bt2.isAppkill()) { // also open the home screen to make music app revert to // background Intent startMain = new Intent(Intent.ACTION_MAIN); startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(startMain); // now we can kill the app is asked to final String kpackage = bt2.getPname(); CountDownTimer killTimer = new CountDownTimer(6000, 3000) { @Override public void onFinish() { try { stopApp(kpackage); } catch (Exception e) { e.printStackTrace(); Log.e(LOG_TAG, "Error " + e.getMessage()); } } @Override public void onTick(long arg0) { if (am2.isMusicActive()) { // for more stubborn players, try this too... Intent downIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON, null); KeyEvent downEvent2 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP); downIntent2.putExtra(Intent.EXTRA_KEY_EVENT, downEvent2); sendOrderedBroadcast(downIntent2, null); } try { stopApp(kpackage); } catch (Exception e) { e.printStackTrace(); Log.e(LOG_TAG, "Error " + e.getMessage()); } } }; killTimer.start(); } } </code>
Aquí está la función stopApp ().
<code>protected void stopApp(String packageName) { Intent mIntent = getPackageManager().getLaunchIntentForPackage( packageName); if (mIntent != null) { try { ActivityManager act1 = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE); // act1.restartPackage(packageName); act1.killBackgroundProcesses(packageName); List<ActivityManager.RunningAppProcessInfo> processes; processes = act1.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo info : processes) { for (int i = 0; i < info.pkgList.length; i++) { if (info.pkgList[i].contains(packageName)) { android.os.Process.killProcess(info.pid); } } } } catch (ActivityNotFoundException err) { err.printStackTrace(); Toast t = Toast.makeText(getApplicationContext(), R.string.app_not_found, Toast.LENGTH_SHORT); if (notify) t.show(); } } } </code>
¿Alguien más se ha encontrado con este problema? ¿Cómo puedo detener de forma fiable la aplicación lanzada? Primero necesito hacerlo para hacer una pausa y ponerlo en segundo plano. Ese es el problema que estoy teniendo. Funciona para la mayoría de las situaciones, pero no todas. Algunos casos, Pandora y Spotify no responden al evento clave que se envía y simplemente siguen jugando. Esto mantiene activo el icono de notificación y convierte la aplicación en una actividad de primer plano, por lo que no puedo detenerla.