0

How to detect home activity is launched/displayed using package info. like we get package name by this code:

ActivityManager.RunningTaskInfo localRunningTaskInfo = (ActivityManager.RunningTaskInfo)((ActivityManager)context.getSystemService("activity")).getRunningTasks(1).get(0);
String str1 = localRunningTaskInfo.topActivity.getPackageName();
String str2 = localRunningTaskInfo.topActivity.getClassName();

With this we get all applications package name and class name. how to detect home is displayed?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
yuva ツ
  • 3,707
  • 9
  • 50
  • 78

1 Answers1

0

You can use ActivityManager to get this. Following is the sample code:

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);


 // get the info from the currently running task
 List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

 Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());

 ComponentName componentInfo = taskInfo.get(0).topActivity;
 String packageName = componentInfo.getPackageName();

You will need the following permission on your manifest:

uses-permission android:name="android.permission.GET_TASKS"

Example Project

From logcat you can see the which activity is lanched..

Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • how to check if its home launcher package name like if(packageName.equals(HOME_ACTIVITY_PACKAGE)){} – yuva ツ Oct 24 '13 at 11:09
  • @yuva if you get activity name in log then store that in string and compare with that. – Hariharan Oct 24 '13 at 11:10
  • logcat reading is not supported in android 4.1 and up. that's why i need package name of home launcher.how can i get it for different devices. its different for different devices. – yuva ツ Oct 24 '13 at 11:54
  • @yuva i have checked v4.2.2 device it worked for me.. i am getting result like this.. com.example.gallerypopup.MainActivity – Hariharan Oct 24 '13 at 12:01
  • i done want applications launcher activity.i want package name when i press home button on device we get home screen that package name not specific activity name – yuva ツ Oct 24 '13 at 12:06
  • like on emulator after pressing home button in logcat i'm getting package name as com.android.launcher.its only for emulator and different for other devices. – yuva ツ Oct 24 '13 at 12:09
  • @yuva now i check it it coming. – Hariharan Oct 24 '13 at 12:11
  • You are not getting my question.according to you i'm getting all package name.also package name for home launcher.how can i check its package for home screen.its different for different devices. – yuva ツ Oct 24 '13 at 12:36