开发者

Android Lock Apps

开发者 https://www.devze.com 2023-04-01 01:03 出处:网络
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'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 u开发者_开发技巧ser 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!


No you cannot know when another application is launched without some kind of hack. This 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;
}


there is a way to do so . you can know when a application is launched. you can use packagemanager class to get all the information about any installed and inbuld application . and use the below code to know whwn that application is launched

           @Override
    public void run() {  Log.i("test","detector run");
        try {
            Process process;
            process = Runtime.getRuntime().exec(ClearLogCatCommand);
            process = Runtime.getRuntime().exec(LogCatCommand);
            br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            // Check if it matches the pattern
            while(((line=br.readLine()) != null) && !this.isInterrupted()){

                Log.d("Detector", "RUN"+line);

                // Ignore launchers
                if (line.contains("cat=[" + Intent.CATEGORY_HOME + "]")) continue;

                Matcher m = ActivityNamePattern.matcher(line);
                if (!m.find()) continue;
                if (m.groupCount()<2){
                    // Log.d("Detector", "Unknown problem while matching logcat output. Might be SDK version?");
                    continue;
                }

                if (mListener!=null) mListener.onActivityStarting(m.group(1), m.group(2));

                Log.i("Detector", "Found activity launching: " + m.group(1) + "  /   " + m.group(2));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


You can now use an AccessibilityService that allows you to find out which Activity is at the top.

In the AccessibilityService class:

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        ComponentName componentName = new ComponentName(
                event.getPackageName().toString(),
                event.getClassName().toString()
        );

        ActivityInfo activityInfo = tryGetActivity(componentName);
        boolean isActivity = activityInfo != null;
        if (isActivity) {
            Log.i("CurrentActivity", componentName.flattenToShortString());
        }
    }
}

You will have to turn the Accessibility on in your phone's settings, which can be done by going to Settings > Accessibility > Services > Your App. There are also a couple of permissions you'll have to add in your Manifest. A lot of the information can be found in the Android Developers site: http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消