用户打开另一个应用程序时,如何截获Intent? [英] How can I intercept the Intent when the user open another application?

查看:57
本文介绍了用户打开另一个应用程序时,如何截获Intent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设用户打开设置"应用程序,是否有一种方法可以从我的应用程序服务中拦截"此意图,以便检测到设置"应用程序将要打开?

Let's say the user open "Settings" application, is there a way to "intercept" this intent, from my app's service, in order to detect that "Settings" app is going to be openned?

例如,在SOTI MobiControl应用程序中,您可以(通过Web仪表板)管理已安装(并已注册到服务器)应用程序的用户的权限.如果您不允许某个用户打开设置"应用程序,则当他尝试打开该应用程序时,会出现一个吐司,显示未经授权".他们怎么样?

For instance, in SOTI MobiControl app you can manage (from a web dashboard) the permissions of the user with the app installed (and enrolled to your server). If you don't allow one user to open Settings app, when he tries to open it, a toast appears saying "Unauthorized". How do they that?

推荐答案

有没有一种方法可以从我的应用服务中拦截"此意图,以便检测到将要打开设置"应用?

Is there a way to "intercept" this intent, from my app's service, in order to detect that "Settings" app is going to be opened?

如前所述,不可能拦截启动意图.

As other mentioned before it's not possible to intercept a launch intent.

例如,您可以在SOTI MobiControl应用中进行管理(通过网络仪表板)安装了该应用程序的用户的权限(以及已注册到您的服务器).如果您不允许一个用户打开设置"应用程序,当他尝试打开它时,会出现一个吐司说未经授权".他们怎么样?

For instance, in SOTI MobiControl app you can manage (from a web dashboard) the permissions of the user with the app installed (and enrolled to your server). If you don't allow one user to open Settings app, when he tries to open it, a toast appears saying "Unauthorized". How do they that?

但是可以确定是否打开了一个应用程序并拦截"了该调用.通过拦截,我的意思是画出启动应用程序的屏幕并显示登录屏幕或未经授权的屏幕.

It's however possible to determine if an app is opened and "intercept" that call. By intercept I mean draw over the starting app's screen and present a login screen or a not authorized screen.

我还没有制定出可以在任何Android版本上运行的完整示例,但根据我对AppLocks的研究,我认为它或多或少是这样的:

I haven't worked out a full sample that would work an any Android version but from my research with AppLocks, I'd say it works more or less like this:

在Lollipop之前的Android上,您将使用它来检索正在运行的进程:

On pre-Lollipop Android you'd use this to retrieve the running processes:

    ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo info : manager.getRunningAppProcesses()) {
        Log.e("TAG", "Running process: " + info.processName);
        if ("com.mycompany.mycoolapp".equals(info.processName)) {
           // do stuff...
        }
    }

需要:

<uses-permission android:name="android.permission.GET_TASKS"/>

或者:

    for (ActivityManager.RunningTaskInfo recentTaskInfo : manager.getRunningTasks(100)) {
        Log.e("TAG", "Recent tasks: " + recentTaskInfo.baseActivity.getPackageName());
    }

在Lollipop及更高版本上,您将使用UsageStats确定应用程序是否正在运行:

On Lollipop and higher you'd use UsageStats to determine if an app is running:

    UsageStatsManager usageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, -1);
    long start = cal.getTimeInMillis();
    long end = System.currentTimeMillis();
    List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, start, end);
    for (UsageStats stats : queryUsageStats) {
        Log.e("TAG", "Usage stats for: " + stats.getPackageName());
    }

需要:

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>

我可能会同时使用AlarmManager来执行该重复任务.

I would probably run both using the AlarmManager to perform that recurring task.

我相当确定这是获取正在运行的应用程序列表的两种方法.如果AppLock拒绝了使用情况统计信息的权限,则说明它不再适用于Android 6.0设备.但是,在M之前的设备上,它仍然有效,这表明该应用程序还有另一种获取正在运行的应用程序列表的方法(上述第一个选项).

I'm fairly certain these are the two ways to get the list of running apps. If the permission for usage stats is denied to AppLock it's not working any more an Android 6.0 devices. On pre-M devices it however still works which is an indicator that the app has an alternative way to get the list of running apps (the first option described above).

一旦确定某个应用已启动(我们上次检查该应用正在运行且未在运行),我们就可以接管"屏幕.这就是我的处理方式: http://www.piwai.info/chatheads-basics/

Once it's determined an app has been started (it's running and hasn't been running the last time we checked), we can "take over" the screen. And that's how I'd do it: http://www.piwai.info/chatheads-basics/

当然,这只是基本概念,我敢肯定在实施可靠的解决方案时会遇到一些陷阱,但这应该给您一些开始.

Of course that's just the basic idea and I'm sure there are a couple of pitfalls when implementing a reliable solution but this should give you something to start with.

这篇关于用户打开另一个应用程序时,如何截获Intent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆