Android: Starting An Activity For A Different Third Party App

匿名 (未验证) 提交于 2019-12-03 03:08:02

问题:

I'm working on an app and I want to integrate the Last.fm app into it. Basically, when someone is looking at an artist in my app, I would like to have a button that they can tap to open up Last.fm application with the artist's information.

This intent works, but it loads a menu asking which app I would like to use (Browser or Last.fm):

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

However, I just want to start the Last.fm app and skip the dialog asking which app to use, I thought maybe using the setPackage() method would work like this:

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

But it causes the app to crash:

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

Is it possible to just start the Last.fm app? Here's a copy of Last.fm's AndroidManifest.xml for reference.

Thanks for reading, Tony

回答1:

Yes, it's possible but you need to know the correct component name. Launch the last.fm app regularly and check the logfile for the cmp=... information that's been used when the app is started. Use this as well in your app then.

I start the Z-DeviceTest app from the market from within my app without a problem like this:

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

in my case the info I took from the logcat was:

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

// cmp=zausan.zdevicetest/.zdevicetest

in order to know how to start the app with the right component/class... do the same for the last.fm app

Edit: I've tested to launch Last.fm from my own app, and this works fine without any errors:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!