如何检测Flutter中是否打开了另一个应用程序? [英] How to detect if another application is open in Flutter?

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

问题描述

我想创建一个不鼓励使用社交媒体应用程序的应用程序.就像他们打开Instagram之类的东西一样,Flutter应用程序就会知道.

I want to create an application that discourages the usage of social media applications. Like if they open Instagram or something then the Flutter app will know.

当我打开Flutter应用程序时,如何检测其他应用程序(例如Instagram或Twitter)是否已打开?

How would I detect if another application (Such as Instagram or Twitter) is open when my Flutter app is opened?

如果很难在Flutter中实现此功能,有人可以为此功能建议其他语言或代码吗?

If this is difficult to implement in Flutter, can anyone suggest some other languages or code for this functionality?

谢谢!

推荐答案

至少在Android上可行.我不为IOS编程,所以不能肯定地说.正如@Gaurav指出的那样,该解决方案将需要定制,并且可能并非特定于Flutter.如果您愿意在Flutter之外工作,那是可能的.

It is possible at least on Android. I do not program for IOS so I can't say for sure. As @Gaurav pointed out, the solution will need to be custom-made and it probably won't be specific to Flutter. If you are willing to work outside Flutter than it is possible.

再次,以下解决方案适用于Android设备:

Once again, the following solutions are for Android devices:

解决方案1:由于您以社交媒体应用为目标,因此您可以查找每个应用(例如Facebook,WhatsApp等)的公开意图,以找出它们正在广播的意图的名称.并在您的应用程序中添加广播接收器.许多人会说这不是一个好主意,因为应用程序开发人员自己可以改变意图,并且您不应该听取未声明为公开使用的任何内容.它们可能是正确的,但是此讨论是关于如何检测其他应用程序启动且此方法有效的.

Solution 1: Since you are targeting social media apps you can lookup the public intents of each app (i.e. Facebook, WhatsApp, etc...) find out the names of the intents they are broadcasting and add a broadcast receiver in your application. Many will say this is not a good idea because the app developer's themselves could change the intents and that you shouldn't listen for anything that hasn't been declared for public use. They are probably right, but this discussion is about how to detect the other apps launching and this method works.

简单地Google如何添加广播接收器,可以在此处.

Simply google how to add a broadcast receiver, a good example can be found here.

有些网站包含许多流行的Android应用程序的广播意图数据库,但是如果您找不到某个特定名称的名称,那么我建议您使用ADB来查找它.例如,Facebook为:

There are websites that contain databases for a lot of broadcast intents for popular Android applications, but if you have trouble finding the name of a particular one then I'd suggest using ADB to find it. For example, Facebook would be:

adb shell pm dump com.facebook.katana |grep'过滤器'|切-d''-f 12

adb shell pm dump com.facebook.katana | grep ' filter' | cut -d ' ' -f 12

在某些版本的Android中,广播意图无法正常工作,但是对我自己来说,在应用程序中注册它的效果很好.

In some versions of Android broadcast intents do not work, however registering it in the application has worked pretty well for myself.

一旦正确设置了侦听器,则可以在用户启动应用程序时检测到该应用程序并对其进行相应的处理.

Once you have a listener setup properly, then you can detect the app when the user launches it and process it accordingly.

解决方案2:您可以通过"Process"和/或"PackageManager"监视Android设备上的进程,并查看何时弹出一个社交媒体应用程序.这种方法不是很可靠,因为应用程序一直都在运行,并且仅仅因为它们在进程列表中并不意味着它们正在被积极使用.

Solution 2: You could monitor the processes on the Android device via "Process" and/or "PackageManager" and see when one of the social media applications popup. This method is not very reliable because apps are on all the time and just because they're in the process list doesn't mean they are actively being used.

解决方案3:再次,这是一个非常棘手的"解决方案,但是您可以收听每台设备上的日志.基本上获取控制台日志的输出,阅读最后100行,然后查看应用程序是否正在执行操作.如果是这样,则您可以确定该应用程序是否处于活动状态.该解决方案的最大问题(除了尝试读取所有用户设备上的日志外)是,这不是即时解决方案,并且可能需要特殊特权,具体取决于所使用的设备.您可能还需要设置一个主动侦听和读取日志的服务,这可能会导致大量的电池使用情况.

Solution 3: Once again, this is a very "hacky" solution, but you could listen to the logs on each device. Basically get the output of console logs, read the last 100 lines and see if the app is doing something. If so, then you may be able to determine if the app is active. The biggest problem with this solution (besides trying to read logs on all your user's devices) is that this is not an instant solution and may require special privileges depending on the device you're working on. You would also probably need to setup a service that actively listens and reads the logs, which might cause some significant battery usage.

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

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