Calling an extrenal application

走远了吗. 提交于 2020-01-24 10:49:49

问题


How can I call an external application from my application?

E.g: I need to call Shazam (application) from my app. I can see the package name of the application in the logcat.

will that be useful for any purpose?


回答1:


You can call a third party application's activity in the following way.

final Intent shazamIntent = new Intent("com.shazam.android");                
shazamIntent .setComponent(new  ComponentName("com.shazam.android","com.shazam.android.Splash"));
startActivity(shazamIntent );

But, this is not a great way to go about it. In case the package name changes (which is a very remote possibility) or the activity name changes (Splash could change to something else) your application would break. If Shazam has an Intent which can be invoked to start listening to a song use that (not sure if they have one).

Also, do necessary check in case Shazam is not installed so that your call doesn't crash.




回答2:


Create the Application's launcher intent object and say startActivity.




回答3:


Specifically for Shazam, the following code works:

Intent intent = new Intent("com.shazam.android.intent.actions.START_TAGGING");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if(!context.getPackageManager().queryIntentActivities(intent, 0).isEmpty()) {
    context.startActivity(intent);
} else {
    // Shazam is not installed
}

START_TAGGING is the intent which is issued when you tap the Shazam widget.



来源:https://stackoverflow.com/questions/4791826/calling-an-extrenal-application

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