A can get all installed web browsers by using this code:
ArrayList<String> allLaunchers = new ArrayList<String>();
Intent allApps = new Intent(Intent.ACTION_MAIN);
List<ResolveInfo> allAppList = getPackageManager().queryIntentActivities(allApps, 0);
for(int i =0;i<allAppList.size();i++) allLaunchers.add(allAppList.get(i).activityInfo.packageName);
Intent myApps = new Intent(Intent.ACTION_VIEW);
myApps.setData(Uri.parse("http://www.google.es"));
List<ResolveInfo> myAppList = getPackageManager().queryIntentActivities(myApps, 0);
for(int i =0;i<myAppList.size();i++){
if(allLaunchers.contains(myAppList.get(i).activityInfo.packageName)){
Log.e("match",myAppList.get(i).activityInfo.packageName+"");
}
}
link: Android Fetch installed and default browser in device
Based on package name, is there a way to get BOOKMARKS URI for each browser?
When I got list of browsers, by package name (via above method), I need to get a link for it's bookmarks (browsing history) database, for example:
On Chrome, package name is com.android.chrome and history URI is content://com.android.chrome.browser/bookmarks, but this case is special one, when only .browser was added to real package name... in most cases it differs totally. Like in FF, where package name is org.mozilla.firefox, and history uri differs: content://org.mozilla.firefox.db.browser/bookmarks (yes, I know that It can't be read in FF), but was only an example.
So basically the question is: Is there a way, knowing this: org.mozilla.firefox, to get this: content://org.mozilla.firefox.db.browser/bookmarks.
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.
Currently I have written a code to load all capable applications which can view images from the phone.
public static List<String> getAllCapableForFileViewing (Context context, String mimeType) {
List<String> packages = new ArrayList<>();
PackageManager pm = context.getPackageManager();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_VIEW);
sendIntent.setType(mimeType);
List<ResolveInfo> resolveInfoList = context.getPackageManager()
.queryIntentActivities(sendIntent, 0);
for (ResolveInfo resolveInfo : resolveInfoList) {
packages.add(resolveInfo.activityInfo.packageName);
System.out.println(resolveInfo.activityInfo.packageName);
System.out.println(resolveInfo.activityInfo.applicationInfo.className);
System.out.println(resolveInfo.activityInfo.name);
System.out.println("");
}
return packages;
}
When I tried to list all the applications, one of them have two set listed e.g. WeChat, WeChat Moment. Obviously it have two activities which can handle the image for viewing. The problem is the name of the two are the same "WeChat".
Additionally? even though it can consume the content i passed in but they are not really application for viewing images e.g. Gallery application. Is there a way to recognise them. I know it may be impossible.
The problem is the name of the two are the same "WeChat".
Well, in the end, that is up to the developers of that app. However, look at labelRes of the ResolveInfo, as this may be a better label to use (e.g., pulled from the <intent-filter>).
Is there a way to recognise them
You are welcome to try using CATEGORY_APP_GALLERY, though this may lead to false negatives (i.e., apps that the user would expect to show up that do not).
In Samsung devices com.sec.android.email is the default In-Built Mail client, but in HTC it is com.htc.android.mail.. My question is is there any way to get the default mail client package name in android device irrespective of the different company builds..
This isn't a complete answer, but here is how to get a list of Activities that can send message/rfc822:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
PackageManager pkgManager = context.getPackageManager();
List<ResolveInfo> activities = pkgManager.queryIntentActivities(intent, 0);
You can iterate over the list. See ResolveInfo documentation for fields of interest.
I'm opening a PDF file through an intent; in order to check beforehand if a capable application is installed, I use this code:
PackageManager packageManager = context.getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List<ResolveInfo> list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
This works fine on my Galaxy Nexus, however, the list is empty when run on a Kindle Fire. But if I skip the check, the PDF is opened just fine! How so?
Is there any other way to check for PDF viewing capablities? (The PDF is included in the APK asset's folder, and I would like to only copy it into the data folder if I know it can be displayed...)
Or should I just stop bothering to check as most Android devices can view PDFs anyway?
I'm fairly sure you can't query for that intent without setting the actual data the Intent should be able to operate on.
This link also seems to deal with the same problem.
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.