应用程序无法从最近使用的应用程序列表中正确关闭?

时间:2019-06-24 18:07:55

标签: android xamarin xamarin.android

当我们通过导航到最近使用的应用程序列表并滑走特定的应用程序或单击“全部关闭”按钮来关闭应用程序时,它会以某种实际上无法使用的怪异状态复活。在这种状态下,应用程序图标带有红色的1徽章图标。

简而言之,我们看到的是在“关闭”滑动应用程序后,该应用程序仍然有效。只能通过设置->应用,选择应用,然后点击“强制停止”来关闭它!

以下内容似乎可行,但仅在通过USB连接到调试器时有效。从设备运行时(即使使用此“强制停止”之类的代码),在从最近的应用程序列表中关闭该应用程序后,我们仍然会看到该应用程序。

ActivityManager am = (ActivityManager)GetSystemService(
    Context.ActivityService);
am.KillBackgroundProcesses("TurkeyanaCall.TurkeyanaCall");
Android.OS.Process.SendSignal(Android.OS.Process.MyPid(), Signal.Kill);

对此有什么解决办法吗?这个错误状态会导致我们的用户界面中断,并且蓝牙交互出现故障。

* EDIT:尝试清除服务并使用System.exit(0),但似乎仍然遇到相同的错误状态

ActivityManager am = (ActivityManager)GetSystemService(
    Context.ActivityService);
IList<ActivityManager.RunningServiceInfo> runningServices = am.GetRunningServices(Java.Lang.Integer.MaxValue);
int pid = Process.MyPid();
foreach (ActivityManager.RunningServiceInfo serviceInfo in runningServices)
{
    if (serviceInfo.Pid == pid)
    {
        try
        {
            Intent intent = new Intent();
            intent.SetComponent(serviceInfo.Service);
            _Instance.StopService(intent);
        }
        catch (Java.Lang.Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

am.KillBackgroundProcesses("TurkeyanaCall.TurkeyanaCall");
Android.OS.Process.SendSignal(Android.OS.Process.MyPid(), Signal.Kill);
Java.Lang.JavaSystem.Exit(0);

0 个答案:

没有答案