android - 将 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 播放。首先我需要打开应用程序然后开始播放。
- 好的 Google 打开 Audible
- 好的 谷歌播放
因此,我想做的事情是可能的。但我不知道“OK Google play”在表面下叫什么让现在的玩家焕然一新。
解决方案
//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)
推荐阅读
- javascript - moment.js 如何在毫秒部分跳过零?
- javascript - 从数组中填充变量的更好方法(解构赋值)
- python - 如何在 Pandas Python 中将此嵌套数组 JSON 响应列转换为 DataFrame
- python - 将预测的随机森林结果转换为数据框
- java - Maven 中是否有“npm outdated”/“npm audit”的等价物?
- laravel-backpack - 是否可以删除 Laravel Backpack 的“表格”字段上的“删除”和“添加 x”?
- google-compute-engine - 通过 Compute Engine VM 的默认凭据使用 GAM(Google Apps Manager)
- linux - 如何从 linux 终端中删除错误?
- c# - 无法以正确的方式反序列化我的 xml 文件。总是为空
- angular - 'mat-expansion-panel' 不是已知元素