首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止或暂停Pandora和Spotify

如何停止或暂停Pandora和Spotify
EN

Stack Overflow用户
提问于 2012-04-26 09:18:05
回答 3查看 5.4K关注 0票数 3

我有一个应用程序,它具有启动应用程序、潘多拉工作站或快捷方式的功能。这一切都很好。稍后,我想停止我启动的应用程序。这对大多数东西都有效,除了Pandora和Spotify并不总是关闭。有时他们会这样做,但并不总是如此。这似乎与当前的UI状态有关。例如,当我显示Pandora或显示主屏幕时,它工作得很好。当Home Dock或Car Mode处于活动状态时,它不起作用。您可以在这里看到我的所有源代码:http://code.google.com/p/a2dpvolume/ service.java就是具有此功能的文件。

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

代码语言:javascript
复制
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()。

代码语言:javascript
复制
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不会响应正在发送的关键事件,它们只是继续播放。这会使notify图标保持活动状态,并使应用程序成为前台活动,因此我无法停止它。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-28 10:33:36

我终于发现,当Pandora看到耳机断开连接时,它确实会暂停音乐。所以,我只需要发送断开连接的意图,这样Pandora就会暂停。一旦暂停,它就能够被推到后台并被杀死。

代码语言:javascript
复制
//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);
票数 3
EN

Stack Overflow用户

发布于 2013-11-09 08:15:16

对于任何其他尝试这样做的人;除非您以系统身份运行,否则不再允许广播android.intent.action.HEADSET_PLUG意图。

票数 2
EN

Stack Overflow用户

发布于 2016-02-07 05:21:38

由于"HEADSET_PLUG“意图现在只有在被系统调用时才被支持,我发现应用程序特定的意图是可行的:

代码语言:javascript
复制
           Intent pauseSpotify = new Intent("com.spotify.mobile.android.ui.widget.PLAY");
           pauseSpotify.setPackage("com.spotify.music");
           sendBroadcast(pauseSpotify);

本质上,它的作用是在spotify应用程序中调用"PLAY“。

我从一台article上得到了这个想法,并将其应用于普通的安卓系统。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10326017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档