【问题标题】:Android Lock Apps安卓锁应用
【发布时间】:2011-08-30 18:38:09
【问题描述】:

我是新来的,我已经搜索过可以帮助我的问题,但我没有明确的答案。

我需要创建一个应用程序来阻止手机上的其他应用程序。 我在市场上见过几个,但我想做一个。 有没有办法知道用户何时尝试打开应用程序并提出活动? (输入密码)。

我尝试使用 FileObserver,但仅适用于文件和目录(显然)。 我可以在启动之前创建一个侦听器来捕获其他应用程序的 Intent 吗?

我为我的英语道歉,感谢您的帮助!

【问题讨论】:

  • 每秒检查一次的服务将在几个小时内耗尽您的电池电量。做一个启动器/家庭应用不是更好吗?
  • 你是对的!但这不是我想要的。感谢您的回答!

标签: android


【解决方案1】:

不,如果没有某种 hack,您无法知道另一个应用程序何时启动。 这是因为应用程序启动没有广播。

您可以做的是创建一个以固定时间间隔(例如 1000 毫秒)运行的服务,以检查前面的非系统应用程序。杀死该应用程序并从服务中弹出一个密码输入框。如果该密码正确,请重新启动该应用程序

这是一些代码示例

    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);

这里是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;
}

【讨论】:

  • 感谢 Weakwire!这真的很有用!我需要一些工作来做我想做的事,但这是一般的想法!谢谢!
  • 这是不完整的方法,因为“//kill the app”没有可行的解决方案。
  • 为了杀死另一个应用程序,您的应用程序必须在前台运行。然后,您可以使用其包名称终止该应用程序。查看stackoverflow.com/questions/23927405/…
  • @weakwire ...感谢这个好答案。我想知道除 Timer 之外的另一种重复运行代码的方法,因为 Timer 在我多次检查该代码后被系统销毁。提前致谢。
【解决方案2】:

有办法做到这一点。您可以知道应用程序何时启动。 您可以使用 packagemanager 类来获取有关任何已安装和内置应用程序的所有信息。并使用以下代码了解该应用程序何时启动

           @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();
        }
    }

【讨论】:

  • 我正在尝试使用此代码,但我做错了什么。请您提供更多代码来使用它。谢谢。截图:oi57.tinypic.com/28b8gmd.jpg
  • @anuj sharma 它在果冻上的工作一直在及以上.....我已经对其进行了测试,我的代码甚至可以在 android L 中完美运行
【解决方案3】:

您现在可以使用 AccessibilityService 来找出哪个 Activity 位于顶部。

在 AccessibilityService 类中:

@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());
        }
    }
}

您必须在手机设置中打开辅助功能,这可以通过转到设置 > 辅助功能 > 服务 > 您的应用来完成。您还必须在清单中添加一些权限。很多信息都可以在 Android 开发者网站上找到:http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html

希望这会有所帮助!

【讨论】:

  • 感谢您的好主意。你能多解释一下tryGetActivity(componentName)方法吗?
猜你喜欢
  • 1970-01-01
  • 2014-01-09
  • 2014-01-23
  • 2012-10-08
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多