How to find installed applications in Android? - android

I want to get the name and package name of a third party application installed in Android.
I have tried but I got the name of all applications (third party and pre installed).
How can I identify whether an application is a system application or an other application?

This will do the trick...cheers :)
PackageManager pm = getPackageManager();
List<PackageInfo> list = pm.getInstalledPackages(0);
for (PackageInfo pi : list) {
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(pi.packageName, 0);
System.out.println(">>>>>>packages is<<<<<<<<" + ai.publicSourceDir);
// this condition if satisfied means the application currently refered by ai
// variable is
// a system application
if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Log.d(getClass().getSimpleName(), ">>>>>>packages is system package" + pi.packageName);
}
} catch (NameNotFoundException e) {
Log.d(getClass().getSimpleName(), "Name not found", e);
}
}

Goto DDMS Perspective ---> Click on the Emulator or Device ---> Check File Explorer ---> data --> data folder ---> there will be app names installed, note their package name :)
Hope it helps

Related

How to open an installed app from another application using voice command on android

So I am trying to build a virtual assistant on android. Currently i am trying to implement the logic to open another app using voice command. I saw this question and tried implementing the same code but it is giving me an exception saying Unknown package: PackageName. So if I say "open facebook" then it says Unknown package: facebook. I can't figure out why this code is not working!
try {
if(text.contains("open") || text.contains("launch") || text.contains("play")) {
speech = text.split(" ", 2);
String appName = speech[1];
PackageManager pm = getPackageManager();
String pkgName = pm.getInstallerPackageName(appName);
Intent launchApp = getPackageManager().getLaunchIntentForPackage(pkgName);
startActivity(launchApp);
}
} catch (Exception ex) {
Toast.makeText(MainActivity.this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
Edit: I got help from answer # 6 on the question which i mentioned.

How to get package name from apk in Android?

I am developing an app which will contain a list of Apps. On click the user will be redirected to the Play Store to download this app. On successful download I have to send that apps package name to a server to validate it. How can I do that?
I assume you want to do this at runtime, so your app can read its own package_id w/o having this hardcoded. For that you need to use PackageManager's getPackageInfo() method:
protected String getPackageName() {
try {
PackageInfo packageInfo = getPackageManager.getPackageInfo(getPackageName(), 0);
return packageInfo.applicationInfo.packageName;
} catch (Exception e) {
e.printStacktrace();
}
return null;
}
Use apkanalyzer, its part of the Android studio:
apkanalyzer manifest application-id path/to/apk/file.apk

Check which app is doing what

Is there any way to check which app is doing what in android, from my application.
I mean
if any app is reading call logs
if any app is reading msg
if any app is reading data from server
if any app is accessing gps,
if any app is playing music
Please suggest any idea, any help will be appreciated.
The best you can do is check if an app has the permissions required to do each task. The following snippet retrieves the permissions for all installed Applications:
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
Log.d("test", "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);
try {
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
//Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
if(requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
Log.d("test", requestedPermissions[i]);
}
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
Beyond that, just because an app has permission to do something doesn't mean it is doing that. Some permissions cover more than one use, and the app could be using one part of what it has access to.

How can i get the apk file name and path programmatically?

I want to get the exact file name of a program if I already know the package name of the target apk. For instance, if I know the package name of my apk, which is com.packagename, how can I get the exact path and file name of that package? Btw, i don't want to get just MY apk location, i want the location of any package name i apply. SystemTuner pro is able to do this so i know it is possible, just not sure how.
Thanks guys!
/**
* Get the apk path of this application.
* #param context any context (e.g. an Activity or a Service)
* #return full apk file path, or null if an exception happened (it should not happen)
*/
public static String getApkName(Context context) {
String packageName = context.getPackageName();
PackageManager pm = context.getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
String apk = ai.publicSourceDir;
return apk;
} catch (Throwable x) {
}
return null;
}
EDIT
In defense of catch (Throwable x) in this case. At first, now it is well-known that Checked Exceptions are Evil. At second, you cannot predict what may happen in future versions of Android. There already is a trend to wrap checked exceptions into runtime exceptions and re-throw them. (And a trend to do silly things that were unthinkable in the past.) As to the children of Error, well, if the package manager cannot find the apk that is running, it is the kind of problems for which Errors are thrown. Probably the last lines could be
} catch (Throwable x) {
return null;
}
but I do not change working code without testing it.
PackageManager.getPackageInfo() returns information about the package, and PackageInfo.applicationInfo field has required information about the application.
Well, i would like to mark Yuri as the answer but i already knew about that stuff. So I went through each and every option from PackageManager.ApplicationInfo and found .publicSourceDir
So a complete answer with code to my question would be
PackageManager pm = getPackageManager();
try {
ApplicationInfo ai = pInfo.getApplicationInfo(<packageName here>, 0);
String sourceApk = ai.publicSourceDir;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
So thanks again guys, got my brain goin once again Love StackOverflow!
in above answer need change pInfo to pm
like this
PackageManager pm = getPackageManager();
try {
ApplicationInfo ai = pm.getApplicationInfo(<packageName here>, 0);
String sourceApk = ai.publicSourceDir;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this answer by Seth

Obtain the Linux UID of an Android App

I would like to be able to get the Linux UID (user ID) of an installed Android application.
Excerpt from Security and Permissions: "At install time, Android gives each package a distinct Linux user ID. The identity remains constant for the duration of the package's life on that device."
Is there a way to retrieve this UID?
adb shell dumpsys package com.example.myapp | grep userId=
Use PackageManager and getApplicationInfo().
The ā€¨packages.xml file present in /data/system
The packages.list file present in /data/system
Contain the list of applications installed and their corresponding UID's.
Use android.os.Process.myUid() to get the calling apps UID directly.
Using the PackageManager is not necessary to find the own UID.
PackageManager packageManager = getPackageManager();
try {
applicationId = String.valueOf(packageManager.getApplicationInfo("com.example.app", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
As CommonsWare already wrote, you can use PackageManager to get the UID.
Here's an example:
int uid;
try {
ApplicationInfo info = context.getPackageManager().getApplicationInfo(
context.getPackageName(), 0);
uid = info.uid;
} catch (PackageManager.NameNotFoundException e) {
uid = -1;
}
Log.i(LOG_TAG, "UID = " + uid);

Categories

Resources