How to get metadata from ActivityInfo? - android

I want to read activity metadata from another app. I have cole like this:
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(MY_ACTION);
List<ResolveInfo> pluginsInfo = packageManager.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
for (ResolveInfo plugin : pluginsInfo) {
ResolveInfo info = plugin;
ActivityInfo aInfo = info.activityInfo;
Bundle metadata = aInfo.metaData;
}
And sometimes it works good, but sometimes metadata is null. On same emulator and same app version. Is there other way to do this?

And answer is simple:
packageManager.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER | PackageManager.GET_META_DATA);

Related

How can I get OS default apps in android

I'm developing a launcher app, I need to retrieve an Android OS default Phone app, Browser app and SMS apps', application Info (Application name, Package name, Launcher icon). Following code is used to get all launchable applications.
private static List<ApplicationInfo> getInstalledApps(Context context, PackageManager pm) {
List<ApplicationInfo> installedApps = context.getPackageManager().getInstalledApplications(0);
List<ApplicationInfo> laughableInstalledApps = new ArrayList<>();
for(int i =0; i<installedApps.size(); i++){
if(pm.getLaunchIntentForPackage(installedApps.get(i).packageName) != null){
laughableInstalledApps.add(installedApps.get(i));
}
}
return laughableInstalledApps;
}
After spending some time with the code, I found a way get what I wanted.
Default Dial App
Intent mainIntent = new Intent(Intent.ACTION_DIAL, null);
mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
ActivityInfo info = pkgAppsList.get(0).activityInfo;
Default SMS App
String smsPkgName = Telephony.Sms.getDefaultSmsPackage(context);
ApplicationInfo info = getPackageManager().getApplicationInfo(smsPkgName, 0);
Default Browser App
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
ResolveInfo resolveInfo = getPackageManager().resolveActivity(browserIntent,
PackageManager.MATCH_DEFAULT_ONLY);
ActivityInfo info = resolveInfo.activityInfo;
Try with PackageManager#getPreferredActivities(java.util.List, java.util.List, java.lang.String), where the first parameter is a list of IntentFilter, which you would like to get default apps for. Then the answer is written in list passed as second parameter.
Here are some common intents for which you might try to find default apps.

Usage of Application Info , Package Info and Resolve Info

What is the use of Application info and how to use it.
I'm confused how to retrieve the installed apps from the device and to display it with logo
Can anyone please help me to sort it out.
Differentiate the terms applicaiton info, package info and resolve info.
to display a list of installed apps, you can try this
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
You will get all the necessary data in the ResolveInfo to start a application. You can check ResolveInfo
or try this code
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>)
pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
}

Getting The Package name out of Activity on Android

I am trying to get the list of the installed browsers on my android. I found a code that provide me the list of activities that handle URL:
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : list) {
String name = info.name;
String pkgName = info.resolvePackageName;
}
I am able to get the activity names, but the package name is always null.
Is there a way to get the package name? or is there a better way to do that ?
Thanks,
RC
You will need to match it with packageInfo objects.
List<PackageInfo> temp = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);
for(PackageInfo info: temp) {
String pkg = info.packageName
}
Try this:
for (ResolveInfo info : list) {
String name = info.activityInfo.name;
String pkgName = info.activityInfo.applicationInfo.packageName;
}

Android check if application package is launchable

I'm looking for the best way to check if application is launchable.
There is my code :
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if (packageManager.getLaunchIntentForPackage(p.applicationInfo.packageName) != null) {
// Get application info
}
}
This works, but when i do app profiling i noticed that packageManager.getLaunchIntentForPackage() method consumes a lot of execution time, so i'm looking for an alternative way to check if each application is launchable without getting the launch intent.
Any idea ?
Thank you !
I found solution for my own problem, i hope it will help someone :
PackageManager packageManager = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = packageManager.queryIntentActivities(
mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(
packageManager));
for (ResolveInfo resolveInfo : apps) {
// Get application data here
}

How do I start a service based on a ServiceInfo object?

In my app, I query for the list of services that have a specific Category in their intent-filters. This goes just fine, I get back a List containing ResolveInfo objects. In these ResolveInfos I found the "serviceInfo" field, that's supposed to describe the details of a found service.
Now how do I construct an Intent from the serviceInfo, that can start the found service?
My code now is like this:
PackageManager pm = getApplicationContext().getPackageManager();
Intent i = new Intent();
i.setAction("<my custom action>");
i.addCategory("<my custom category>");
List<ResolveInfo> l = pm.queryIntentServices(i, 0);
gatherAgentNum = l.size();
if(gatherAgentNum > 0){
for(ResolveInfo info : l){
Intent i2 = new Intent(this, info.serviceInfo.getClass());
i2.putExtra(BaseAgent.KEY_RESULT_RECEIVER, new GatherResult(mHandler));
startService(i2);
}
}
This's obviously wrong, the "info.serviceInfo.getClass()" just returns the serviceInfo object's class. Could anyone help me with this?
Thank you
Edit: The solution (at least the one I used):
PackageManager pm = getApplicationContext().getPackageManager();
Intent i = new Intent();
i.setAction("<my action>");
i.addCategory("<my category>");
List<ResolveInfo> l = pm.queryIntentServices(i, 0);
if(l.size() > 0){
for(ResolveInfo info : l){
ServiceInfo servInfo = info.serviceInfo;
ComponentName name = new ComponentName(servInfo.applicationInfo.packageName, servInfo.name);
Intent i2 = new Intent();
i2.setComponent(name);
startService(i2);
}
}
Have you taken a look at :
http://developer.android.com/reference/android/content/pm/ServiceInfo.html
http://developer.android.com/reference/android/content/pm/PackageItemInfo.html#packageName
http://developer.android.com/reference/android/content/pm/ApplicationInfo.html
I guess you could try to replace the getClass class with .packageName - and use packageManager.getLaunchIntentForPackage(String)
Also take a look at:
http://developer.android.com/reference/android/content/pm/PackageManager.html
queryIntentServices, resolveService

Categories

Resources