I need to develop app just like android App lock that opens password activity when app assigned application get launch. I have read below question but didn't find proper solution. I just want to ask that what service or method they are using? how to know about any other app launch event?
In my current implementation, I have added a service and in MyService, I have added a timer to check currentApp and then I am comparing current app to assigned app.
I don't like this approach because timer takes lot of battery as well as device get slow.
public void isForeground(String myPackage){
String currentApp="";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager usm = (UsageStatsManager) getSystemService("usagestats");
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
time - 1000 * 1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(),
usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
currentApp = mySortedMap.get(
mySortedMap.lastKey()).getPackageName();
}
}
} else {
ActivityManager am = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
currentApp = am.getRunningTasks(1).get(0).topActivity .getPackageName();
}
processCurrentApp(currentApp,myPackage);
}
Any one can help me in this? Thanks in advance.