Some Android devices don't have Android Market, like Korea, etc.
Is it possible to detect the existence of Android Market at runtime?
I know I can try to open a market uri first to see if there is any exception thrown. But I don't think this is a wise approach.
I know I can try to open a market uri
first to see if there is any exception
thrown.
Create the ACTION_VIEW Intent for the Market Uri, then use PackageManager and queryIntentActivities() to see if you get anything back. If that returns an empty list, you know nothing on the device handles Market Uri values.
A complete solution, that also reads the packagename by itself ...
Context context = this;
final PackageManager packageManager = context.getPackageManager();
String packagename = this.getPackageName();
String url = "market://details?id=" + packagename;
Intent i2 = new Intent(Intent.ACTION_VIEW);
i2.setData(Uri.parse(url));
List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(i2,PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.size() > 0) startActivity(i2);
Related
I'm developing an app that allows users to take photos, draw on them, and then upload them into our social media application. I would like to use a third-party app for the drawing to lessen the work load on us. I found a number of answers on here about how to do so, and much of the following code is taken from those answers. When I try different combinations of answers, I always get Toast errors on the Intent. The two errors I get are "File could not be opened" and "Image could not be edited". The former sounds like file permissions problems, which I think should be getting resolved from the grantUriPermissions work-around below. The other error I have no idea about.
// Code taken from answer on http://stackoverflow.com/questions/15699299/android-edit-image-intent
final Uri uri = Uri.parse(this.photoPath);
int flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION;
Intent editIntent = new Intent(Intent.ACTION_EDIT);
editIntent.setDataAndType(uri, "image/*");
editIntent.addFlags(flags);
editIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
// This work-around allows the intent to access our private FileProvider storage.
// Code taken from http://stackoverflow.com/questions/24835364/android-open-private-file-with-third-party-app
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(editIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, uri, flags);
}
startActivityForResult(Intent.createChooser(editIntent, null), EDIT_INTENT);
Anybody seen these Toast messages before and been able to resolve them?
I figured out the problem. My use of Uri.parse was wrong because I'm using a FileProvider in my application. Exchanging that line for FileProvider.getUriForFile made the app work as intended.
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).
I am trying to get the package name of the default android clock (stock clock).
This is the intent I use to open the stock clock.
Intent i = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
I have tried
i.getPackage();
But this returns null. Is there a way to get the package name of the default clock in android, in any phone?
But this returns null
That is because you have not set the package name in the Intent.
Is there a way to get the package name of the default clock in android, in any phone?
Not really, as there is no concept of a "default clock" in Android.
However, you can use PackageManager and queryIntentActivities() to see what activities will respond to the Intent that you have constructed.
Hope this will help you:
final PackageManager packageManager = this.getPackageManager();
Intent intent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
List<ResolveInfo> packages = packageManager.queryIntentActivities(intent,0);
for(ResolveInfo res : packages){
String package_name = res.activityInfo.packageName;
Log.w("Package Name: ",package_name);
}
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.
How could I write a code which can tell me that android market is installed on your android phone?
There are two ways. You can use the already mentioned getPackageManager() and getApplicationInfo() (if the package is not found, a PacketManager.NameNotFoundException will be thrown - see here). Android Market's package name is com.android.vending.
However, you can also create a dummy intent for searching the market and check how it is handled. If the resulting list has at least one entry, you can be sure that Android Market is installed:
Intent market = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=dummy"));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(mmarket, 0);
Here's what I did (assumes browser exists):
Intent market = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("market://details?id=com.example.app"));
Intent website = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("http://play.google.com/store/apps/details?id=com.example.app"));
try {
startActivity(market);
} catch (ActivityNotFoundException e) {
startActivity(website);
}