Getting The Package name out of Activity on Android - 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;
}

Related

How to get Package name from activity name

I use the code below to get the launcher activity name belongs to specific package name:
Intent intent = new Intent();
intent.setPackage(aPackageName);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ResolveInfo result = getPackageManager().resolveActivity(intent, 0);
I save result.activityInfo.name to shared preference
Later I want to start this activity, but how to get its package name?
or, Is it possible to start this activity without knowing the package name it belongs to?
Intent launchIntent =
getPackageManager().getLaunchIntentForPackage(How to get the package name);
if(launchIntent != null){
startActivity(launchIntent);
}
Knowing that the activity(s) name(s) that I save are not mine.
To answer this,
How to get Package name from activity name
If you want to find the package name from the Launcher activity name, please check the following,
String activityName = "TermuxActivity";
PackageManager pm = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0);
Collections.sort(activityList, new ResolveInfo.DisplayNameComparator(pm));
for (ResolveInfo temp : activityList) {
if(temp.activityInfo.name.endsWith(activityName)){
Log.i("ActivityCheck", " Activity : " +temp.activityInfo.name+ " package name: " +temp.activityInfo.packageName);
}
}
Output:
ActivityCheck: Activity : com.termux.app.TermuxActivity package name: com.termux
Try this :-
public static String PACKAGE_NAME;
PACKAGE_NAME = getApplicationContext().getPackageName();
You can use this given below code to get package name of application
In Java supported code:
String packageName=this.getPackageName(); // where this represent the context
In Kotlin supported code:
var packageName:String=this.packageName // where this represent the context
Use the following code to get the launcher activity of all packages from your installed apps:
final PackageManager pm = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));
for (ResolveInfo temp : appList) {
Log.v("my logs", "package and activity name = "
+ temp.activityInfo.packageName + " "
+ temp.activityInfo.name);
}
The only reliable way was Vladyslav Matviienko suggestion, which is storing package name along with each activity name in a hashmap to be like .. Thank you all for your help.

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.

How to get metadata from ActivityInfo?

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);

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 to obtain the main activity name in application?

How to get the main activity in third party applications of system?
PackageManager pm= getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
for (PackageInfo pi : packs) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("icon", pi.applicationInfo.loadIcon(pm));
map.put("appName", pi.applicationInfo.loadLabel(pm));
map.put("packageName", pi.packageName);
}
I know how to get the package name. But do not know how to obtain the name of main activity.
I use pi.activities[0].name. It still obtain null pointer.
Anyone’s idea is very appreciate.
use
pm.getLaunchIntentForPackage(pi.packageName);
This will give you launch intent, if you print it you will see the launcher intent along with the main activity.
If you want to show only activity that can be launched from your device launcher then you need to put a filter for Category Launcher.
You can use the following
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "unknown)");

Categories

Resources