How to get Package name from activity name - android

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.

Related

Android get Activity with intentfilter launcher from package name programatically

I have this package name from a third party application:
"com.example.packagename"
This application has a activity with an intentFilter with a category Launcher:
<category android:name="android.intent.category.LAUNCHER"/>
How can I retrieve programmatically this activity name from the package name?
Finding the laucher activity of the third party Termux app(package name : "com.termux").
Snipplet : Approach 1
If you want the activity name and component names,
String packageName = "com.termux";
Intent i= getPackageManager().getLaunchIntentForPackage(packageName);
if(i != null && i.getComponent()!=null){
Log.i("Activity", " Activity getComponent : " +i.getComponent().toString());
Log.i("Activity", " Activity getClassName: " +i.getComponent().getClassName());
Log.i("Activity", " Activity getShortClassName : " +i.getComponent().getShortClassName());
} else{
Log.i("Activity", " Activity not found");
}
Output:
Activity getComponent : ComponentInfo{com.termux/com.termux.app.TermuxActivity}
Activity getClassName: com.termux.app.TermuxActivity
Activity getShortClassName : .app.TermuxActivity
Snipplet: : Approach 2:
PackageManager pm = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage("com.termux");
List<ResolveInfo> activityList = pm.queryIntentActivities(intent, 0);
Collections.sort(activityList, new ResolveInfo.DisplayNameComparator(pm));
for (ResolveInfo temp : activityList) {
Log.i("Activity", " Activity : " +temp.activityInfo.name);
}
Output:
Activity: Activity : com.termux.app.TermuxActivity
Note:
If you want to start the Launcher activity of a package,
String packageName = "com.termux";
Intent i = getPackageManager().getLaunchIntentForPackage(packageName);
if(i != null){
startActivity(i);
} else{
Log.i("Activity", "package not found, ensure the "+packageName+" is installed.");
}
If you want to find the package name from the Launcher activity name,
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

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.

Deleting android apps from activity

Good day!
Are there some tools in Android SDK, that i can use to remove application from activity. In particular, i need activity method, that removes other application with the same app-name, but other package.
If you mean with "same name app" to app with same label that define in XML menifest as the label of your app, this snippest should work:
private void deleteAppByActivityName(#NonNull String myAppLabel,#NonNull Context context){
try {
PackageManager pm = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> dataInDevice = pm.queryIntentActivities(mainIntent, 0);
for (ResolveInfo resolveInfo : dataInDevice){
String label = resolveInfo.loadLabel(pm).toString();
if (label.equals(myAppLabel)) { //we find app with same name as ours
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + resolveInfo.activityInfo.packageName));
context.startActivity(intent);
break;
}
}
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
}

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

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