潘多拉开始从Android应用程序背景 [英] Start pandora in the background from android app

查看:234
本文介绍了潘多拉开始从Android应用程序背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过语音命令启动潘多拉Android应用程序。它的伟大工程,但我想活动切换回我的应用程序,让潘多拉在后台运行。我使用这个code发动潘多拉:

I have an android application that starts pandora by voice command. It works great, but I want the activity to switch back to my application, leaving pandora running in the background. I'm using this code to launch pandora:

PackageManager pm = getPackageManager()
   try{
       String packageName = "com.pandora.android";
       launchIntent = pm.getLaunchIntentForPackage(packageName);

       startActivity(launchIntent);
      }
   catch (Exception e1)
   {}

有什么想法?

推荐答案

第一眼看我想你可以用 Activity.startActivities ,如:

First glance I thought you could use Activity.startActivities, as in:

    final PackageManager pm = getPackageManager();
    try {
        startActivities(new Intent[] {
                pm.getLaunchIntentForPackage("com.pandora.android"),
                pm.getLaunchIntentForPackage("com.your.packagename")
        });
    } catch (final Exception ignored) {
        // Nothing to do
    } finally {
        finish();
    }

但潘多拉需要一点时间才能开始播放,并在这种情况下,我想你最好的选择是建立一个<一个href="https://developer.android.com/reference/android/service/notification/NotificationListenerService.html"相对=nofollow> NotificationListenerService 并等待被张贴潘多拉的通知,这显示播放已启动,然后启动您的应用程序。

But Pandora needs a little time to start playback and in that case, I think you best bet is to set up a NotificationListenerService and wait for Pandora's notification to be posted, which would indicate playback has started, then launch your app.

下面是一个例子:

public class PandoraNotificationListener extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        final String packageName = sbn.getPackageName();
        if (!TextUtils.isEmpty(packageName) && packageName.equals("com.pandora.android")) {
            startActivity(new Intent(this, YourActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // Nothing to do
    }

}

在AndroidManifest

<service
    android:name="your.path.to.PandoraNotificationListener"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

此外,用户将需要启用您的应用程序监听到被张贴在通知:

Also, your users will need to enable your app to listen for notifications to be posted under:

  • 设置 - >安全 - >通知接入

不过你可以通过引导用户直有以下意图

But you can direct your users straight there by using the following Intent:

startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

这篇关于潘多拉开始从Android应用程序背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆