Please suggest me how can I get current open application name, even if there is home screen on device then I will find \"Home screen is open\".
Use an AccessibilityService
I tested and working in Android 2.2 (API 8) through Android 7.1 (API 25).
public class MyAccessibilityService extends AccessibilityService {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
//Configure these here for compatibility with API 13 and below.
AccessibilityServiceInfo config = new AccessibilityServiceInfo();
config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
if (Build.VERSION.SDK_INT >= 16)
//Just in case this helps
config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(config);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Log.d("ABC-",accessibilityEvent.getPackageName()+" -- "+accessibilityEvent.getClassName());
if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
if (accessibilityEvent.getPackageName() != null && accessibilityEvent.getClassName() != null) {
ComponentName componentName = new ComponentName(
accessibilityEvent.getPackageName().toString(),
accessibilityEvent.getClassName().toString()
);
ActivityInfo activityInfo = tryGetActivity(componentName);
boolean isActivity = activityInfo != null;
if (isActivity)
Log.i("CurrentActivity", componentName.flattenToShortString());
}
}
}
private ActivityInfo tryGetActivity(ComponentName componentName) {
try {
return getPackageManager().getActivityInfo(componentName, 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
@Override
public void onInterrupt() {
}
}
For me the above example did not work. So i ended up using this:
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RecentTaskInfo> l = am.getRecentTasks(1,
ActivityManager.RECENT_WITH_EXCLUDED);
Iterator<ActivityManager.RecentTaskInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
try {
Intent intent = i.next().baseIntent;
List<ResolveInfo> list = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
list.get(0).activityInfo.packageName,
PackageManager.GET_META_DATA));
Toast.makeText(this, "Application name: " + c.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this,
"Application name not found: " + e.toString(),
Toast.LENGTH_LONG).show();
}
}
Here you go
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
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
}
}
You can also list running tasks with the code below:
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
List li = am.getRunningTasks(100);
Iterator i = li.iterator();
PackageManager pm = getApplicationContext().getPackageManager();
while (i.hasNext()) {
try {
ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo)(i.next());
String ac = info.baseActivity.getPackageName();
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
ac, PackageManager.GET_META_DATA));
Log.v("asd", c.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
Simply using this code
getActivity().getApplicationInfo().packageName
It gives you application info, and then call the package name.
With this you can get the current application name
Resources appR = getApplicationContext().getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName()));
System.out.println(txt+" APp Name");