Good day. Can I use the android sdk to get the name of an android application in which the user is working, and the session-time in this application. i.e. service will receive the name of the application and a session and send it to the database.
Solved. Here is my soulution
private String getRunningAppByActivity(){
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
String name=componentInfo.getPackageName();
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(name, 0);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
Log.d("APPLICATION_NAME", applicationName);
return applicationName;
}