-->

如何停止或暂停潘多拉和Spotify(How to stop or pause Pandora an

2019-07-29 04:16发布

我有一个有一个功能,启动应用,Pandora电台,或快捷方式的应用程序。 这一切工作正常。 后来我想停下来,我开始了应用。 这适用于除Pandora和Spotify的大多数事情并不总是紧密。 有时他们做,但并非总是如此。 这似乎是与当前的UI状态。 举例来说,当我有潘多拉的表现或主屏幕显示效果还算不错。 当家庭Dock或租车模式有效时,它不起作用。 你可以在这里看到我所有的源代码: http://code.google.com/p/a2dpvolume/ service.java是具有此功能的文件。

下面是试图停止播放音乐,然后停止应用程序代码的一部分。

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();

        }
    }

下面是函数stopApp()。

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();
        }

    }
}

已经有人遇到这个问题? 我如何能可靠地停止发布应用程序? 我需要先得到它的暂停,并把它的背景。 这是我遇到的问题。 它适用于大多数情况下,但不是全部。 某些情况下,Pandora和Spotify的将不会对关键事件作出回应被发送,他们只是玩下去。 这使通知图标活跃,使得应用前景的活性,所以我不能阻止它。

Answer 1:

我终于想通了,潘多拉不暂停音乐时,看到一个断开耳机。 所以,我不得不发送断开意图使潘多拉将暂停。 暂停后,它能够被推向背景和杀害。

//Try telling the system the headset just disconnected to stop other players
            Intent j = new Intent("android.intent.action.HEADSET_PLUG");
            j.putExtra("state", 0);
            sendBroadcast(j);


Answer 2:

对于其他人尝试这一点; 该android.intent.action.HEADSET_PLUG意图不再被允许,除非你正在运行的系统广播。



Answer 3:

好象叫通过系统的“HEADSET_PLUG”的意图,现在只支持,我发现应用程序特定意图是要走的路:

           Intent pauseSpotify = new Intent("com.spotify.mobile.android.ui.widget.PLAY");
           pauseSpotify.setPackage("com.spotify.music");
           sendBroadcast(pauseSpotify);

从本质上讲,这是什么呢,是它调用从Spotify应用“PLAY”。

我从上心的文章 ,并把它应用到正常的机器人。



文章来源: How to stop or pause Pandora and Spotify