So stoppen oder unterbrechen Sie Pandora und Spotify

Ich habe eine App mit einer Funktion zum Starten einer App, einer Pandora-Station oder einer Verknüpfung. Das alles funktioniert gut. Später möchte ich die von mir gestartete App stoppen. Dies funktioniert für die meisten Dinge, mit Ausnahme von Pandora und Spotify, die nicht immer geschlossen werden. Manchmal tun sie das aber nicht immer. Es scheint mit dem aktuellen Status der Benutzeroberfläche zu tun zu haben. Zum Beispiel funktioniert es einwandfrei, wenn Pandora oder der Startbildschirm angezeigt wird. Wenn das Home Dock oder der Auto-Modus aktiv sind, funktioniert dies nicht. Sie können meinen gesamten Quellcode hier sehen:http://code.google.com/p/a2dpvolume/ service.java ist die Datei mit dieser Funktionalität.

Hier ist der Teil des Codes, der versucht, die Musikwiedergabe zu stoppen und dann die App zu stoppen.

<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>

Hier ist die Funktion 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>

Ist jemand anderes auf dieses Problem gestoßen? Wie kann ich die gestartete App zuverlässig stoppen? Ich muss es erst zum Anhalten bringen und in den Hintergrund stellen. Das ist das Problem, das ich habe. Es funktioniert in den meisten Situationen, aber nicht in allen. In einigen Fällen reagieren Pandora und Spotify nicht auf das gesendete Schlüsselereignis und spielen einfach weiter. Dadurch bleibt das Benachrichtigungssymbol aktiv und die App wird zu einer Vordergrundaktivität, sodass ich sie nicht stoppen kann.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage