gpt4 book ai didi

Android:为不同的第三方应用程序启动 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:59 54 4
gpt4 key购买 nike

我正在开发一个应用程序,我想将 Last.fm 应用程序集成到其中。基本上,当有人在我的应用程序中查看艺术家时,我希望有一个按钮,他们可以点击该按钮以打开包含艺术家信息的 Last.fm 应用程序。

此 Intent 有效,但它会加载一个菜单,询问我想使用哪个应用程序(浏览器或 Last.fm):

Intent i = new Intent();
i.setData(Uri.parse("http://last.fm/music/" + headliner));
i.setAction("android.intent.action.VIEW");
startActivity(i);

但是,我只想启动 Last.fm 应用程序并跳过询问使用哪个应用程序的对话框,我认为使用 setPackage() 方法可能会像这样工作:

i.setPackage("fm.last.android");

但它会导致应用程序崩溃:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://last.fm/music/Rihanna pkg=fm.last.android }

是否可以只启动 Last.fm 应用程序? Here's Last.fm 的 AndroidManifest.xml 的副本以供引用。

感谢阅读,托尼

最佳答案

是的,这是可能的,但您需要知道正确的组件名称。定期启动 last.fm 应用程序并检查日志文件中的 cmp=... 应用程序启动时使用的信息。然后在您的应用中也使用它。

我在我的应用程序中从市场启动 Z-DeviceTest 应用程序时没有出现这样的问题:

final Intent intentDeviceTest = new Intent("android.intent.action.MAIN");                
intentDeviceTest.setComponent(new ComponentName("zausan.zdevicetest","zausan.zdevicetest.zdevicetest"));
startActivity(intentDeviceTest);

在我的例子中,我从 logcat 中获取的信息是:

// dat=content://applications/applications/zausan.zdevicetest/zausan.zdevicetest.zdevicetest

// cmp=zausan.zdevicetest/.zdevicetest

为了知道如何使用正确的组件/类启动应用程序...对 last.fm 应用程序执行相同的操作

编辑:我已经测试过从我自己的应用程序启动 Last.fm,这工作正常,没有任何错误:

final Intent intentDeviceTest = new Intent("android.intent.action.MAIN");                
intentDeviceTest.setComponent(new ComponentName("fm.last.android","fm.last.android.LastFm"));
startActivity(intentDeviceTest);

关于Android:为不同的第三方应用程序启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3518407/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com