Android,检测其他应用何时启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3290936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android, Detect when other apps are launched
提问by Ian
I'm trying to develop an app that prevents a user from getting to a specified app without a password. The scenario is...
我正在尝试开发一个应用程序,以防止用户在没有密码的情况下访问指定的应用程序。情节是...
- user clicks on "Email" app (for example)
- my app detects launch of an app
- my app confirms it is the "Email" app
- my app opens a view over the top, asking for a password
- user enters a password, if correct, my app disappears, leaving the "Email" app on top
- 用户点击“电子邮件”应用(例如)
- 我的应用程序检测到应用程序的启动
- 我的应用程序确认它是“电子邮件”应用程序
- 我的应用程序在顶部打开一个视图,要求输入密码
- 用户输入密码,如果正确,我的应用程序消失,将“电子邮件”应用程序留在顶部
I'm ok doing the rest of it, just part 2 is puzzling me, and after many days reading up on Broadcast Intents etc and trying to listen for "android.intent.action.MAIN" etc in my trial projects I can't seem to detect when an app other than mine is started.
我可以完成剩下的工作,只是第 2 部分让我感到困惑,经过多天阅读 Broadcast Intents 等内容并尝试在我的试用项目中收听“android.intent.action.MAIN”等内容后,我不能似乎检测到我以外的应用程序何时启动。
Can anyone help? Am I going about it the right way, in looking for new apps broadcasting an intent to start, or should I be reading the system log for new intents, or doing something in native code?
任何人都可以帮忙吗?我是否以正确的方式寻找新的应用程序来广播启动意图,或者我应该阅读系统日志以获取新意图,还是在本机代码中执行某些操作?
Any pointers would help, even if you can't answer it fully I'll be able to do some more research. Thanks a lot. Ian
任何指示都会有所帮助,即使您无法完全回答,我也可以做更多的研究。非常感谢。伊恩
采纳答案by M.Movaffagh
I think we can use logcat
and analyze it's output.
我认为我们可以使用logcat
和分析它的输出。
In all similar programs I have found this permission :
在所有类似的程序中,我都找到了这个权限:
android.permission.READ_LOGS
android.permission.READ_LOGS
It means all of them use it but it seems the program starts and after that our program (app protector) will start and bring front.
这意味着他们所有人都使用它,但似乎程序启动,之后我们的程序(应用程序保护程序)将启动并显示在前面。
Use below code :
使用以下代码:
try
{
Process mLogcatProc = null;
BufferedReader reader = null;
mLogcatProc = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});
reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
log.append(line);
log.append(separator);
}
String w = log.toString();
Toast.makeText(getApplicationContext(),w, Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
And do not forget to add it's permission in Manifest file.
并且不要忘记在清单文件中添加它的权限。
回答by Aimeric
A gimmicky way to do it is have a service with a timed loop that checks
一种花哨的方法是使用带有定时循环的服务来检查
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
You run through that list to look at what is running on the phone. Now you can identify them with ids and processName, so for standard activity this is easy for custom ones well unless you stop them all its hard to discriminate...
您浏览该列表以查看手机上正在运行的内容。现在你可以用 ids 和 processName 来识别它们,所以对于标准活动,这对于自定义活动来说很容易,除非你阻止它们很难区分......
Note:this isnt a list of whats is actually on the screen, just a list of whats is running...kinda nullifying your goal maybe but at least you will know when something is starting to run... it will keep being in that list even when in background though.
注意:这不是屏幕上实际显示的内容列表,只是正在运行的内容列表......也许有点使你的目标无效,但至少你会知道什么时候开始运行......它会一直在那里即使在后台列出。
For the password thing you can just start your activity when you found an app thats protected or whatever.
对于密码问题,您可以在找到受保护的应用程序或其他任何应用程序时开始您的活动。
回答by Pontus Gagge
I think and hope this is not possible. Consider how easily such functionality could be abused by malicious software. You can listen to intents directed at you, and those that are broadcast, but application launching should not be a broadcast event.
我认为并希望这是不可能的。考虑一下恶意软件可以多么容易地滥用此类功能。您可以收听针对您的意图以及广播的意图,但应用程序启动不应是广播事件。
What you may be able to do is replace the launcher. If the user agrees to it.
您可以做的是更换启动器。如果用户同意。
回答by Veaceslav Gaidarji
class CheckRunningActivity extends Thread{
ActivityManager am = null;
Context context = null;
public CheckRunningActivity(Context con){
context = con;
am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}
public void run(){
Looper.prepare();
while(true){
// Return a list of the tasks that are currently running,
// with the most recent being first and older ones after in order.
// Taken 1 inside getRunningTasks method means want to take only
// top activity from stack and forgot the olders.
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
String currentRunningActivityName = taskInfo.get(0).topActivity.getClassName();
if (currentRunningActivityName.equals("PACKAGE_NAME.ACTIVITY_NAME")) {
// show your activity here on top of PACKAGE_NAME.ACTIVITY_NAME
}
}
Looper.loop();
}
}
You can get current running Activity
and check if this Activity
corresponds to Email
application.
您可以获取当前运行Activity
并检查这是否Activity
与Email
应用程序相对应。
Run CheckRunningActivity
Thread
on Application
start (or on device boot).
CheckRunningActivity
Thread
在Application
启动时运行(或在设备启动时运行)。
new CheckRunningActivity().start();
Update:This class need android.permission.GET_TASKS
permission, so add next line to the Manifest:
更新:这个类需要android.permission.GET_TASKS
权限,所以在清单中添加下一行:
<uses-permission android:name="android.permission.GET_TASKS" />
回答by CodeFusionMobile
The main issue is you are trying to listen for implicit intents when the Launcher (home screen) is typically using explicit intents.
主要问题是当启动器(主屏幕)通常使用显式意图时,您正在尝试侦听隐式意图。
An implicit intent is when you want to say "Somebody play this video" and Android picks an app that can handle that intent.
隐式意图是当您想说“有人播放此视频”并且 Android 选择可以处理该意图的应用程序时。
An explicit intent is what happens when you click the "Email" icon on the home screen. It is specifically telling Android to open that specific app by fully qualified name (i.e. com.android.mail or something).
当您单击主屏幕上的“电子邮件”图标时,会发生明确的意图。它专门告诉 Android 通过完全限定名称(即 com.android.mail 或其他名称)打开该特定应用程序。
There is no way AFAIK to intercept such explicit intents. It is a security measure built into Android that no two Activities can have the same fully qualified package name. This prevents a third party from cloning the app and masquerading as that app. If what you wish to do was possible, you could theoretically install an app that could block all of your competition's apps from working.
AFAIK 无法拦截这种明确的意图。这是 Android 内置的一项安全措施,任何两个活动都不能具有相同的完全限定包名称。这可以防止第三方克隆应用程序并伪装成该应用程序。如果您希望这样做是可能的,理论上您可以安装一个应用程序,该应用程序可以阻止您的所有竞争对手的应用程序工作。
What you are trying to do goes against the Android security model.
您尝试执行的操作与 Android 安全模型背道而驰。
One thing you could do is partner with specific app developers to forward the intents to your security system, but that's probably not something you want to deal with.
您可以做的一件事是与特定的应用程序开发人员合作,将意图转发到您的安全系统,但这可能不是您想要处理的事情。
回答by Plo_Koon
getRunningTasks()
is deprecated in Android L.
getRunningTasks()
在 Android L 中已弃用。
To obtain app usage statistics you can use UsageStatsclass from android.app.usagepackage.
要获取应用使用统计信息,您可以使用android.app.usage包中的UsageStats类。
The new App usage statistics API allows app developers to collect statistics related to usage of the applications. This API provides more detailed usage information than the deprecated getRecentTasks() method.
新的应用程序使用统计 API 允许应用程序开发人员收集与应用程序使用情况相关的统计信息。此 API 提供比已弃用的 getRecentTasks() 方法更详细的使用信息。
To use this API, you must first declare the android.permission.PACKAGE_USAGE_STATS
permission in your manifest. The user must also enable access for this app through Settings > Security > Apps with usage access
.
要使用此 API,您必须首先android.permission.PACKAGE_USAGE_STATS
在清单中声明权限。用户还必须通过 启用对此应用程序的访问Settings > Security > Apps with usage access
。
Hereis a basic app example showing how to use App usage statistics API to let users collect statistics related to usage of the applications.
这是一个基本的应用程序示例,展示了如何使用应用程序使用统计 API 来让用户收集与应用程序使用相关的统计信息。
回答by Jacob Malliet
Perhaps you need a service, something that will run in the background constantly. Than have your service do what you said. Listen for the android.intent.action.MAIN also with the category android.intent.category.LAUNCHER. Then have that broadcast receiver override the onReceive method and do check to see the name of the application etc.
也许您需要一项服务,该服务将持续在后台运行。比让你的服务做你说的。使用类别 android.intent.category.LAUNCHER 收听 android.intent.action.MAIN。然后让广播接收器覆盖 onReceive 方法并检查应用程序的名称等。