简体   繁体   中英

Finding the main activity for an Android application

I have a custom launcher and at times want to add a button to launch third party applications. To do this I need the apps package name, which is easy to find, but also the apps main or starting activity. I use this to create Intents that I pass to my app.

For example, to create an intent that launches Skype I use:

`new Intent().setComponent(new ComponentName("com.skype.raider", "com.skype.raider.Main"))

I was able to find the name of the main activity by Googling around and as Skype is a popular app it showed up eventually.

I know that if I have the APK I can just open it and look at the androidmainifest , however I'm not sure how to get the APK's of some apps at all. For instance, I want to start this app from Google Play . I can see the package name, com.dbapp.android.mediahouse , however I'm not sure how to get the name of the main activity(I tried .MAIN).

I've looked at the Chrome extension apk downloader that supposendly allowed you to get APK's from Google Play but it appears to no longer work.

I know starting third party apps from a custom launcher is something that must be fairly common, but I'm not finding the answer when I search.

How do people typically handle this issue? Do you get the APK from the Google Play Store and if so how does one do that? Again, I know that I can get the activity from reading the manifest but I don't know how to get the APK itself in many cases. Any advice would be very much appreciated, thanks!

A launcher should be finding the MAIN / LAUNCHER activities advertised by apps on the device, by using PackageManager and queryIntentActivities() . Here is a sample app that implements such a launcher.

PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);

main.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);

You then use that List<ResolveInfo> to display the available activities in a ListView , GridView , etc.

PackageManager提供了一种获取所需内容的方法:

PackageManager.getLaunchIntentForPackage(String packageName)

You can use Intent intent = new Intent(android.content.Intent.MAIN);

or like the example behind to launch Maps and parse a URI:

String uri = "http://maps.google.com/maps?saddr=" + Utils.getLatitude(ShowDetails.this)+","+Utils.getLongitude(ShowDetails.this)+"&daddr="+userLatitude+","+userLongitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM