首页 > 解决方案 > 将 Intent 过滤器操作传递给 Audible

问题描述

我正在尝试从应用程序中打开 Audible 并在启动时开始播放 Audible。Audible 启动正常,但不开始播放。

Audible Manifest 文件包含:

<receiver
android:name="com.audible.application.WidgetReceiver"
android:enabled="true"
android:exported="false">
  <intent-filter>
    <action android:name="com.audible.application.app.PAUSE" />
    <action android:name="com.audible.application.app.PLAY" />        
    ...
  </intent-filter>
</receiver>

我已经创建了一个 Intent 并将 Action 设置为 Manifest 文件中指定的那个。但是,Audible 启动但不开始播放。

Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent =  getPackageManager().getLaunchIntentForPackage("com.audible.application");
                if (intent != null) {
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    // Set Intent Action as defined in Audible Manifest 
                    intent.setAction("com.audible.application.app.PLAY");
                    getApplicationContext().startActivity(intent);
                } else {
                    Toast.makeText(getApplication(), "Cannot launch Audible!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

我还能尝试什么让 Audible 在发布时开始播放?

更新:我已经设法让 Audible 开始使用 OK Google 播放。首先我需要打开应用程序然后开始播放。

因此,我想做的事情是可能的。但我不知道“OK Google play”在表面下叫什么让现在的玩家焕然一新。

标签: android

解决方案


        //open audible
        val intent =
            packageManager.getLaunchIntentForPackage("com.audible.application")
        intent?.let { startActivity(it) }

        //simulate pressing the media play button after 1 second
        Timer().schedule(object : TimerTask() {
            override fun run() {
                (getSystemService(Context.AUDIO_SERVICE) as AudioManager).run {
                    dispatchMediaKeyEvent(
                        KeyEvent(
                            KeyEvent.ACTION_DOWN,
                            KEYCODE_MEDIA_PLAY_PAUSE
                        )
                    )
                    dispatchMediaKeyEvent(
                        KeyEvent(
                            KeyEvent.ACTION_UP,
                            KEYCODE_MEDIA_PLAY_PAUSE
                        )
                    )
                }

            }
        }, 1000)

推荐阅读