考虑到API 29中的新安全性更改,如何将应用返回到前台?

问题描述 投票:0回答:1

我有一个应用程序,该应用程序通过本地主机向不受控制的另一个第三方应用程序发出http请求,并在继续之前等待该调用的响应。工作流程如下:

  • 用户在我的应用内
  • 用户按下按钮,将启动并调出第三方应用程序
  • 用户与第三方应用程序交互
  • [当第三方应用程序完成其工作时,我的应用程序将获取完成的http响应,并通过MoveTaskToFront将自身拉回到最前端,以便用户继续工作。

此功能在Android 9及以下版本中正常运行,但由于new restrictions on launching activities from the background.,因此我认为最后一步在Android 10中不起作用

我无法控制第三方应用程序,因此我无法修改它以使其在完成工作后自行关闭,也无法在适当的时候要求将调用应用程序返回到前台。有人知道解决方法吗?

编辑:根据要求,我在标注中添加了代码段。这是一个Xamarin项目,所以它是用C#编写的,但是此特定代码段是Android平台特定的,因此我可以进行Android系统调用。

首先,我必须启动第三方应用程序:

Intent intent = CrossCurrentActivity.Current.AppContext.PackageManager.GetLaunchIntentForPackage("com.bbpos.android.tsys");
            if (intent != null)
            {
                // We found the activity now start the activity
                intent.AddFlags(ActivityFlags.ClearTask);
                CrossCurrentActivity.Current.AppContext.StartActivity(intent);
            }

然后我通过本地主机调用它,处理响应,并想切换回我的应用程序。

using (var client = new HttpClient())
            {
                // by calling .Result we're forcing synchronicity
                var response = client.GetAsync("http://127.0.0.1:8080/v2/pos?TransportKey=" + pTransportKey + "&Format=JSON").Result;
                if (response.IsSuccessStatusCode)
                {
                    var responseContent = response.Content;

                    // as above, forcing synchronicity
                    string responseString = responseContent.ReadAsStringAsync().Result;

                    var result = JsonConvert.DeserializeObject<GeniusTransactionResponse>(responseString);

                    var manager = (ActivityManager)Application.Context.GetSystemService(Context.ActivityService);
                    var test = manager.AppTasks.First().TaskInfo.Id;
                    manager.AppTasks.First().MoveToFront();
                    //manager.MoveTaskToFront(CrossCurrentActivity.Current.Activity.TaskId, 0);

                    return result;
                }
                else
                {
                    return null;
                }
            }
android
1个回答
0
投票

快速更新,以防其他人遇到相同的问题:我可以通过在项目中添加Accessibility Service来解决此问题。用户只需注册并启用了无障碍服务,即可使MoveTaskToFront像在<29的API中一样起作用。实际的服务不需要执行任何操作。

© www.soinside.com 2019 - 2024. All rights reserved.