简体   繁体   English

Android:当所有程序发送到后台时,如何关闭从我的应用程序打开的应用程序

[英]Android: How to close application opened from my app when all sent to background

Here is my problem. 这是我的问题。 I am developing app that loads some documents from server. 我正在开发从服务器加载一些文档的应用程序。 I open document in another app via Intent.ACTION_VIEW. 我通过Intent.ACTION_VIEW在另一个应用程序中打开文档。 This is all working just fine. 这一切都很好。 Problem is that whole app is pin protected so I have to capture events such "sent to background" or "screen lock" to bring up pin screen afterwards and this is not working when another app is opened above mine. 问题是整个应用程序都受到密码保护,因此我必须捕获诸如“发送到后台”或“屏幕锁定”之类的事件,然后才能显示密码屏幕,而在我的上方打开另一个应用程序时,此功能不起作用。 So if user opens document then press home button, click on my launch icon from menu then he gets again external app with opened document and with back button access my app again. 因此,如果用户打开文档,然后按主页按钮,请从菜单中单击我的启动图标,然后他会再次获得带有打开文档的外部应用程序,并通过后退按钮再次访问我的应用程序。 This is security issue that needs to be fixed. 这是需要修复的安全问题。 Here are some code snippets: 以下是一些代码段:


Opening document: 开启文件:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);

Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivityForResult(intent, 1);

capture sent to background: 捕获发送到后台:

        ActivityManager am = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            IN_BACKGROUND = true;
            Log.i(PinUtil.class.getSimpleName(), "App sent to background ");
        } else {
            IN_BACKGROUND = false;
        }
    }

My question is: Is it possible to detect if my app is sent to background when another app is opened? 我的问题是:是否有可能在打开另一个应用程序时检测我的应用程序是否发送到后台? How not to open another app when my launcher icon is pressed. 按下我的启动器图标后如何不打开另一个应用程序。

Thanks for all responses. 感谢您的所有回复。 Regards Lubos 关于卢博斯

In order to fix this problem: 为了解决此问题:

So if user opens document then press home button, click on my launch icon from menu then he gets again external app with opened document and with back button access my app again. 因此,如果用户打开文档,然后按主页按钮,请从菜单中单击我的启动图标,然后他会再次获得带有打开文档的外部应用程序,并通过后退按钮再次访问我的应用程序。 This is security issue that needs to be fixed. 这是需要修复的安全问题。 Here are some code snippets: 以下是一些代码段:

You need to make sure that, when you launch an external app for the user to view a document, that the external app does not run in the same task as your application. 你需要确保的是,当你启动一个外部应用程序供用户查看文档时,外部应用程序不会在同一个任务,你的应用程序运行。 It needs to run in a new, separate task. 它需要在新的单独任务中运行。 You can do this like this: 您可以这样做:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Ensure app runs in separate task
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivity(intent); // Can't use startActivityForResult() here

However, you can't use startActivityForResult() when you launch the external viewer, because an activity running in another task cannot return a result to you. 但是,启动外部查看器时不能使用startActivityForResult() ,因为在另一个任务中运行的活动无法将结果返回给您。 However, most external applications won't return a result when launched with ACTION_VIEW anyway, so it probably isn't a problem. 但是,大多数外部应用程序无论如何用ACTION_VIEW启动时都不会返回结果,因此这可能不是问题。

Then you asked this: 然后你问这个:

My question is: Is it possible to detect if my app is sent to background when another app is opened? 我的问题是:是否有可能在打开另一个应用程序时检测我的应用程序是否发送到后台? How not to open another app when my launcher icon is pressed. 按下我的启动器图标后如何不打开另一个应用程序。

There should be some answers on StackOverflow that can help you determine if your application is in the background (it isn't actually that easy to determine this). 在StackOverflow上应该有一些答案,可以帮助您确定应用程序是否在后台(确定它实际上并不那么容易)。

My explanation above should answer your 2nd question. 我上面的解释应该回答您的第二个问题。 If you don't launch other apps in your task, then only your app will be launched when your launcher icon is pressed. 如果您未在任务中启动其他应用程序,则在按下启动器图标时仅将启动您的应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在后台关闭我的 android 应用程序的所有活动 - How i can close all activities of my android app in background 如何在Android中以编程方式通过我的应用程序关闭所有最近的应用程序 - How to close all Recent App through my app programmatically in android 如何查看来自我的Android应用程序的已发送电子邮件? 还是打算完成(无论发送了什么电子邮件)? - How can I check sent emails from my Android application? Or intent upon completion (regardless of email sent)? 当您的应用程序从通知中打开时,是否可以从打开的位置返回到以前的应用程序? 安卓 - When your application is opened from notification is it possible to get back to previous application from where it was opened? Android 如何关闭Selenium中所有打开的驱动程序? - How to close all opened drivers in Selenium? 如何关闭postgres打开的​​所有端口? - How to close the all ports opened by postgres? 如何关闭由 Selenium 打开的所有 Chrome windows - How to close all Chrome windows opened by Selenium 如何计算任何应用程序在我的 android 手机中打开的次数? - How to count number of times any application opened in my android mobile? 退出应用程序时是否应该关闭所有资源? - Should I close all of my resources when exiting the application? 计算我的Android应用程序已打开的次数 - Counting how many times my Android app has been opened
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM