I'm new here and I've searched for questions to help me but I have no clear answers.
I need to make an application to block other applications on the phone. I've seen several on the market but I want to make one. is there any way of knowing when a user tries to open an application and bring forward an activity? (to put the password).
I tried with FileObserver, but only works with files and directories (obviously). Could I make a listener that captures the Intent of the other applications before starting?
I apologize for my english and I appreciate your help!
Source: Tips4all, CCNA FINAL EXAM
No you cannot know when another application is launched without some kind of hack.
ReplyDeleteThis is because application launches are not broadcasted.
What you can do is creating a service running on fixed intervals , say 1000 milliseconds, that checks what non system application is on front. Kill that app and from the service pop a password input box. If that password is correct relaunch that application
Here is some code sample
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
List<RunningAppProcessInfo> appProcesses= activityManager.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
try {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (!lastFrontAppPkg.equals((String) appProcess.pkgList[0])) {
apkInfo = ApkInfo.getInfoFromPackageName(appProcess.pkgList[0], mContext);
if (apkInfo == null || (apkInfo.getP().applicationInfo.flags && ApplicationInfo.FLAG_SYSTEM) == 1) {
// System app continue;
} else if (((apkInfo.getP().versionName == null)) || (apkInfo.getP().requestedPermissions == null)) {
//Application that comes preloaded with the device
continue;
} else {
lastFrontAppPkg = (String) appProcess.pkgList[0];
}
//kill the app
//Here do the pupop with password to launch the lastFrontAppPkg if the pass is correct
}
}
}
} catch (Exception e) {
//e.printStackTrace();
}
}
}
}, 0, 1000);
And here is the ApkInfo.getInfoFromPackageName()
/**
* Get the ApkInfo class of the packageName requested
*
* @param pkgName
* packageName
* @return ApkInfo class of the apk requested or null if package name
* doesn't exist
* @see ApkInfo
*/
public static ApkInfo getInfoFromPackageName(String pkgName,
Context mContext) {
ApkInfo newInfo = new ApkInfo();
try {
PackageInfo p = mContext.getPackageManager().getPackageInfo(
pkgName, PackageManager.GET_PERMISSIONS);
newInfo.appname = p.applicationInfo.loadLabel(
mContext.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(mContext
.getPackageManager());
newInfo.setP(p);
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
return newInfo;
}
That service that checks every second will drain your battery in a couple hours.
ReplyDeleteWouldn't it be better to make a launcher / home app?