Android get package name of current opened application
getting current package name in android
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
// display the current class name
Toast.makeText(getApplicationContext(), taskInfo.get(0).topActivity.getClassName(), Toast.LENGTH_LONG).show();
ComponentName componentInfo = taskInfo.get(0).topActivity;
// current class package name
String packageName = componentInfo.getPackageName();
add this to your project manifest permissions
<uses-permission android:name="android.permission.GET_TASKS"/>
Get active Application name in Android
Did you try using ActivityManager.getRunningAppProcesses()
?
Here is the sample code for retrieving names:
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
}catch(Exception e) {
//Name Not FOund Exception
}
}
Getting the name of the application that is running in the foreground : android
This method will be helpful , ActivityManager.getRunningAppProcesses()
check this ,
Get active Application name in Android
How to `getTopActivity` name or get currently running application package name in lollipop?
try this:
ActivityManager mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
}
else{
String mpackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
we can get using UsageStats:
public static String getTopAppName(Context context) {
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String strName = "";
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
strName = getLollipopFGAppPackageName(context);
} else {
strName = mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName();
}
} catch (Exception e) {
e.printStackTrace();
}
return strName;
}
private static String getLollipopFGAppPackageName(Context ctx) {
try {
UsageStatsManager usageStatsManager = (UsageStatsManager) ctx.getSystemService("usagestats");
long milliSecs = 60 * 1000;
Date date = new Date();
List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, date.getTime() - milliSecs, date.getTime());
if (queryUsageStats.size() > 0) {
Log.i("LPU", "queryUsageStats size: " + queryUsageStats.size());
}
long recentTime = 0;
String recentPkg = "";
for (int i = 0; i < queryUsageStats.size(); i++) {
UsageStats stats = queryUsageStats.get(i);
if (i == 0 && !"org.pervacio.pvadiag".equals(stats.getPackageName())) {
Log.i("LPU", "PackageName: " + stats.getPackageName() + " " + stats.getLastTimeStamp());
}
if (stats.getLastTimeStamp() > recentTime) {
recentTime = stats.getLastTimeStamp();
recentPkg = stats.getPackageName();
}
}
return recentPkg;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
// TO ENABLE USAGE_STATS
// Declare USAGE_STATS permisssion in manifest
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
how to get package name of application being displayed on screen in a foreground service, android?
Well, below API-Level 21 or loolipop, you can get the whole package with the activity class, but from API-22 you can't get the whole application package with activity because of some security reasons.
As you know that Android is not going to be a secure phone, so that's why activity name is restricted.
Related Topics
Why Can't One Add/Remove Items from an Arrayadapter
How to Setup Appium in Ubuntu for Android
Android - Detecting Application Launch from Home or History
Getactionview() of My Menuitem Return Null
How to Hide One Item in an Android Spinner
Waiting for Target Device to Come Online
Am I Getting the Steps Right for Verifying a User's Android In-App Subscription
Download and Extract Zip File in Android
Android Opening a File with Action_Get_Content Results into Different Uri'S
Align Top of Image to Top of Textview
How to Programmatically Uninstall a Package in Android
Android 2.1 View's Getdrawingcache() Method Always Returns Null
Multiple Dex Files Define Lcom/Google/Android/Gms/Internal/Zzau
How to Set Custom Typeface to Items in Navigationview
Android Task Affinity Explanation
How to Implement Multi-Selection and Contextual Actionmode in Actionbarsherlock