Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `getTopActivity` name or get currently running application package name in lollipop?

I am creating a app lock application. How to get current running task in lollipop? getRunningTaskinfo method is deprecated in lollipop API, then how to overcome this problem?

like image 975
No Name Avatar asked Jan 21 '15 11:01

No Name


People also ask

How do I find the package name of an application?

You can find an app's package name in the URL of your app's Google Play Store listing. For example, the URL of an app page is play.google.com/store/apps/details? id=com. example.

What display the name of the current application package?

Title bar. The title bar displays both the name of the application and the name of the spreadsheet.

What is the default package name in Android Studio?

And choose Android Library, the default package name given in the module is always com.

How do I find my kotlin package name?

String packageName = getPackageName();


1 Answers

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); 
like image 151
KomalG Avatar answered Sep 22 '22 07:09

KomalG