I need to know the list of installed applications that can be launched by the user (for example: Browser, Email, Maps, etc.). I read this question about the getInstalledApplications method, so I wrote the following code:
final PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo app : apps) {
Intent launchIntent = pm.getLaunchIntentForPackage(app.packageName);
if (launchIntent != null) {
Log.d(LOG_TAG, "getApplicationLabel: " + pm.getApplicationLabel(app));
Log.d(LOG_TAG, "loadLabel: " + app.loadLabel(pm));
Log.d(LOG_TAG, "packageName: " + app.packageName);
Log.d(LOG_TAG, "name: " + app.name);
}
}
In this way I get the list of applications that can be launched. Each of these applications is characterized by a package name, so if I want to start one of these, just get the launch intent for the package by specifying the package name.
This means that each package has at most an activity that can be launched, so each of the applications (which are returned by the getInstalledApplications method) should have a unique package name. Is that correct?
Android won't typically let you install more than one application using the same package name. In my experience, the .apk file for the second app won't install, let alone run. So, no, you're not going to get more than one application per package name.
It IS possible to have multiple activities be launched via intents from the same application, though. Your code won't get them, because getLaunchIntentForPackage() only returns one intent, but each activity can have its own intent filters. The "Note Pad Example" at http://developer.android.com/guide/topics/intents/intents-filters.html has three different activities, each of which can be launched from the outside.
Related
My app forwards users to a different app to perform a specific action (e.g. ACTION_SHARE, except that the apps that I forward users to do not implement an intent filter) Since they don't implement intent filters, I have a list of package names that support the action.
This part is working fine, like this:
for (String knownApp : knownApps) {
Intent intent = pm.getLaunchIntentForPackage(knownApp);
if (intent != null) {
ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
intentList.add(new LabeledIntent(intent, knownApp, resolveInfo.loadLabel(pm), resolveInfo.icon));
}
}
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
Intent openInChooser = Intent.createChooser(actionIntent, getString(R.string.perform_action_with));
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
If the user has none of the apps installed, I want to give the user a choice of apps to download to fulfill the action.
Currently that looks like this.
As you can see it's lacking both icon and name. For regular apps I use an intent chooser which needs LabledIntent, but on one hand, I can't get the name and icon from the playstore unless I scrape them (which is not allowed by google, besides LabledIntent requires a resourceId as the Icon, which I can't get for downloaded files.), on the other the intent chooser won't seem to display the intent unless the package name of the intent and LabeledIntent match. This does not work for URIs which I'm using to access the Play Store in the first place.
Now I'm looking for ideas on how to get the following code to display both the correct name and app icon, as well as forward to the correct page on the play store.
protected void showPlayStoreOptions(List<String> knownApps) {
Intent chooserIntent = new Intent();
Intent showIntent = Intent.createChooser(chooserIntent, "You need one of these Apps on Google Play..."); //googles brand guidelines state that "on Google Play" has to be used
List<Intent> list = new ArrayList<>();
for (String knownApp : knownApps) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + knownApp)); //normally you would try an uri with market:// first, catch the exception if no playstore is installed and then use this, but the intent chooser seems to automatically forward correctly.
list.add(intent);
//list.add(new LabeledIntent(intent, "https://play.google.com/store/apps/details?id=" + knownApp, "test name", R.drawable.icon_info));
//list.add(new LabeledIntent(intent, ""+Uri.parse("https://play.google.com/store/apps/details?id=" + knownApp), "test name", R.drawable.icon_info));
}
showIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, list.toArray(new Intent[list.size()]));
startActivity(showIntent);
}
So to sum up my questions.
How can I get a resource Id from a downloaded image file, or how can I use the downloaded image file with a LabledIntent.
(Extending LabledIntent does not work due to issues with parceling (and those methods are package private))
How can I display a LabledIntent in a choose intent with an URI?
I realize it's probably easier to write my own chooser, but I want to wrangle this into the default android system.
In my widget, when the widget is opened, I want it to open up a new page that shows links to the users already installed applications. The users must be able to add and delete the links as needed and the links would be updated if that application was deleted from the users device. I have already created the ability in my widget to open a new class but I don't know on how to go about linking to user applications. How can I do this?
You have to get the Activity of the app and link it with URI.
For example:
Uri.parse("market://details?id=" + facebookpackagename);
will load the Facebook app from within your app.
Try this code:
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo:packages){
if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
String currAppName = pm.getApplicationLabel(packageInfo).toString();
//This app is a non-system app
}
else{
//System App
}
}
I would like to start a default application: browser, contact-book, phone, email, music app, etc. I have found many q/a, like browser opening a specific URL or blank, and here the answer is even "No not possible". But I would like to just open/launch it without telling it to go to a specific URL or sending a mail to someone, etc.
However, I also saw some Home applications where this seems to be working (at least for some apps). On my colleague's device there is for example a different contact-book (no google) which is detected and opened correctly.
I have seen in the Android documentation some intent categories that point to these problems, but these are only >= API.11. So I can't use/test them on my device.
Question: Is it not somehow possible to launch a default application (having the app chooser is of course ok) without providing extra data? If no, what do you think are these Home apps doing (perhaps workarounds are somehow possible).
PS: for the phone app I think, I have a workaround using Intent.ACTION_DIAL without any other information which will open simply the dialer.
UPDATE: I modified the title. Some applications like the address book may not be the same on different devices. So in this case I would like to start the address-book app, whichever this is.
This answer is not a 100% answer, but some workarounds on some typical applications.
Still open are: music player, address book
Browser: I get a list of applications that handle "http"-data intents, and then I look if one is available in the list of preferred applications.
Intent appFilter = new Intent(Intent.ACTION_VIEW);
appFilter.setData(Uri.parse("http://www.google.com"));
List<ResolveInfo> browserInfoList = pm.queryIntentActivities(appFilter, 0);
List<IntentFilter> outFilters = new ArrayList<IntentFilter>();
List<ComponentName> outActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(outFilters, outActivities, null);
if(outActivities.size() > 0) {
for(ComponentName cn : outActivities) {
String cnClass = cn.getClassName();
String cnPkg = cn.getPackageName();
for (ResolveInfo info : browserInfoList) {
if(info.activityInfo.name.equals(cnClass) &&
info.activityInfo.packageName.equals(cnPkg)) {
return cn;
}
}
}
}
In case no default is found, I open a browser chooser dialog, see here.
Phone: as described in the question:
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
You can start apps by the function "startActivity" if you know about the canonical app name
like "android.com.browser". Do this simple by searching for AndroidManifest.xml in the app
source code (look at Codeaurora.com or at github/Cyanogenmod) and grab the app name you want.
After you know about the App name ("Activity") implement the code as follows:
Intent intent = new Intent();
intent.setClassName(this, "com.android.browser");
intent.setCategory(Intent.ACTION_MAIN);
startActivity(intent);
THIS is only a example, sometimes you have to put intent extras or data values, this information can be found in the app's AndroidManifest.xml too.
How can I launch a facebook app from my app in android?
Looking at the latest Facebook apk (1.6), it looks like both "facebook://" and "fb://" are registered protocols.
facebook://
facebook:/chat
facebook:/events
facebook:/friends
facebook:/inbox
facebook:/info
facebook:/newsfeed
facebook:/places
facebook:/requests
facebook:/wall
fb://
fb://root
fb://feed
fb://feed/{userID}
fb://profile
fb://profile/{userID}
fb://page/{id}
fb://group/{id}
fb://place/fw?pid={id}
fb://profile/{#user_id}/wall
fb://profile/{#user_id}/info
fb://profile/{#user_id}/photos
fb://profile/{#user_id}/mutualfriends
fb://profile/{#user_id}/friends
fb://profile/{#user_id}/fans
fb://search
fb://friends
fb://pages
fb://messaging
fb://messaging/{#user_id}
fb://online
fb://requests
fb://events
fb://places
fb://birthdays
fb://notes
fb://places
fb://groups
fb://notifications
fb://albums
fb://album/{%s}?owner={#%s}
fb://video/?href={href}
fb://post/{postid}?owner={uid}¹
Sorry if I missed some... only played with a handful of them in the emulator to see if they actually work - a bunch of them will cause the Facebook application to crash.
¹ where postid is in the uid_postid format, e.g 11204705797_10100412949637447
To just start the default Launcher Activity:
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.facebook.katana", "com.facebook.katana.LoginActivity");
startActivity(intent);
I did some research, because I wanted to find this out :). I found some ways how to start different activities easily. But I can not guarantee that this will work after upgrades of facebook. I tested it with my current facebook app and it works. At least I tested it with "adb shell" using "am start .....".
Basic is:
String uri = "facebook://facebook.com/inbox";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
the facebook.com part is not checked. you can even type: "facebook://gugus.com/inbox" having the same effect.
How to do this in adb.
1. Start adb shell through console: "adb shell"
2. run: "am start -a android.intent.action.VIEW -d facebook://facebook.com/inbox"
this will start the inbox activity.
Here some Uris with examples. I think they speak for themselves what they do.
facebook://facebook.com/inbox
facebook://facebook.com/info?user=544410940 (id of the user. "patrick.boos" won't work)
facebook://facebook.com/wall
facebook://facebook.com/wall?user=544410940 (will only show the info if you have added it as friend. otherwise redirects to another activity)
facebook://facebook.com/notifications
facebook://facebook.com/photos
facebook://facebook.com/album
facebook://facebook.com/photo
facebook://facebook.com/newsfeed
there might be additianl parameters you can give to certain of those uris, but I have no time to go through all the code of those activities.
How did I do this? check out apktool.
If any one want to directly open a photo
public Intent getOpenFacebookIntent(String pId) {
try {
activity.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("facebook:/photos?album=0&photo=" + pId + "&user=" + ownerId));
} catch (Exception e) {
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/"));
}
}
startActivity(getOpenFacebookIntent(pid));
where ownerId is the Facebook id of the user who uploaded this picture and pid is PhotoId
Enjoy :))
Launching of another application from your application in android, can be done only if Intent action you fire matches with intent filter of other application you want to launch.
As #patrick demonstrated, download facebook.apk to emulator and try to run that through adb shell command. It works fine..
Pass Intent filter and data as an Uri
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/groups/14531755***6215/")));
This directly opens facebook group having the id: 14531755***6215 inside fb app
I have a question related to choosing an application programmatically when shown the dialog "Complete Action Using" in Android.
An example would be as follows:
In my code, I have this statement:
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=98yl260nMEA")));
I will then be shown a dialog box with two options:
to complete the action using the Browser or YouTube
Any idea how can I choose YouTube without being shown the dialog box?
I think you will need more information about the intent-filter of the app you want to launch by default (in this case youtube app). That target app might have multiple intent-filters and one of them might be more specific. You can call startActivity with that specific intent, and then the intended app will be launched directly. However, this requires you to have more knowledge of the target app (which is difficult in most cases like Youtube app).
Other than that, I don't think you can do much from within your app. Intent resolution is done by the Android framework, so if a user app could override it somehow, that would be a flaw in terms of security.
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(Intent.ACTION_VIEW, Uri.parse(youtube.com/watch?v=Zi_XLOBDo_Y)), 0);
Iterator<ResolveInfo> actList = activities.iterator();
while(actList.hasNext()) {
ResolveInfo curr = actList.next();
Log.d("Intents =====> ", curr.toString() + " " + curr.match + " " + curr.isDefault);
}