如何终止Xamarin应用程序?
如何从任何活动中终止Xamarin应用程序?
我已经尝试了System.Environment.Exit(0)和System.Environment.Exit(1)以及Finish()并杀死了所有活动。
仍然会打开一个空白页面,其中包含默认活动名称和黑屏。
有什么针对性的解决方案吗?
Nik asked 2020-08-06T01:07:33Z
12个解决方案
45 votes
如果您使用的是Xamarin.Forms,请创建一个依赖服务。
接口
public interface ICloseApplication
{
void closeApplication();
}
Android:使用FinishAffinity()无法重启您的活动。 它将仅关闭应用程序。
public class CloseApplication : ICloseApplication
{
public void closeApplication()
{
var activity = (Activity)Forms.Context;
activity.FinishAffinity();
}
}
iOS:如上所述。
public class CloseApplication : ICloseApplication
{
public void closeApplication()
{
Thread.CurrentThread.Abort();
}
}
超人
public class CloseApplication : ICloseApplication
{
public void closeApplication()
{
Application.Current.Exit();
}
}
Xamarin形式的用法
var closer = DependencyService.Get();
closer?.closeApplication();
Akash Amin answered 2020-08-06T01:08:00Z
19 votes
对于Android,您可以
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
iOS明确不为现有应用提供任何API。 只有操作系统可以关闭应用程序。
Jason answered 2020-08-06T01:08:24Z
10 votes
使它跨平台工作的一种简单方法是通过以下命令:
System.Diagnostics.Process.GetCurrentProcess().CloseMainWindow();
从此链接得到它。
编辑:使用了一段时间后,我发现2998733080694894883328不会杀死该应用程序,只会关闭它(很明显)。 如果要终止应用程序(杀死),则应使用以下命令:
System.Diagnostics.Process.GetCurrentProcess().Kill();
vhoyer answered 2020-08-06T01:08:53Z
9 votes
对于iOS,您可以使用以下代码:
Thread.CurrentThread.Abort();
对于Android,如@Jason在此处提及:
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
IdoT answered 2020-08-06T01:09:18Z
4 votes
在您的活动中,使用此代码
this.FinishAffinity();
Lee Yeong Guang answered 2020-08-06T01:09:37Z
2 votes
System.Diagnostics.Process.GetCurrentProcess().Kill();
M. Hamza Rajput answered 2020-08-06T01:09:53Z
1 votes
上述答案的简单的多合一组合,而不是接口/依赖关系:
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
var result = await this.DisplayAlert("Alert!", "want to exit?", "Yes", "No");
if (result)
{
#if __ANDROID__
var activity = (Android.App.Activity)Forms.Context;
activity.FinishAffinity();
#endif
#if __IOS__
Thread.CurrentThread.Abort();
#endif
}
});
return true;
}
AhHatem answered 2020-08-06T01:10:13Z
1 votes
我尝试了这段代码
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
var result = await DisplayAlert("", "Would you like to exit from application?", "Yes", "No");
if (result)
{
if (Device.OS == TargetPlatform.Android)
{
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
}
else if (Device.OS == TargetPlatform.iOS)
{
Thread.CurrentThread.Abort();
}
}
});
return true;
}
在这种情况下,当用户选择终止应用程序时,iOS和Android应用程序将关闭。 也许对您有帮助。
Nitika Chopra answered 2020-08-06T01:10:37Z
1 votes
System.Environment.Exit(0); 为我工作
Dwight answered 2020-08-06T01:10:57Z
0 votes
正如您最初的问题提到活动一样,您的问题专门针对Android,因此您应该牢记这一点来更新问题标题,以避免有人在这里寻找跨平台的解决方案。
对于iOS和Android(例如Xamarin Forms),您可以抛出一个异常,该异常是“繁重的工作”方法,可以完成此工作:
throw new Exception();
由于这不是最佳的用户体验,因此您可能只想在iOS上使用它,因为在Android上,您可能会看到系统弹出窗口,告诉您应用程序已崩溃。 但是,与其他iOS方法(例如通过选择器调用exit(0)或调用私有iOS方法(例如“ terminateWithSuccess”))不同,它不应仅根据您的操作方式使应用商店验证失败。 他们可能仍然无法使您失败,因为您的应用程序试图自行终止。
您可能想为Android实施一些不同的事情,在这种情况下,Jason的回答就足够了。
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
无论哪种方式,您都应该真正质疑为什么需要提供此选项。 与桌面应用程序不同,桌面应用程序需要关闭应用程序,因为应用程序驻留在窗口中,而这些应用程序在设计上允许进行多任务处理,而不是面向任务,因此,移动平台的主要目的是让用户一次专注于一项任务。 用户完成任务后,应决定通过单击主屏幕按钮,后退按钮或更改应用程序(任务)按钮来退出此任务。 这确实适用于所有平台。
Daniel Maclean answered 2020-08-06T01:11:37Z
0 votes
当我尝试从活动B关闭Xamarin Android应用程序并同时在其下打开活动A时,上述方法都没有帮助我完全关闭Xamarin Android应用程序。
一个聪明的家伙在这里留下了诡计
在活动B中首先致电private void CloseBtn_Click(object sender, EventArgs e){
FinishAffinity();
JavaSystem.Exit(0);
}(关闭两个活动,但是,该应用程序仍在后台运行)
然后调用private void CloseBtn_Click(object sender, EventArgs e){
FinishAffinity();
JavaSystem.Exit(0);
}杀死后台应用程序(我认为可以将其替换为Android.OS.Process.KillProcess(Android.OS.Process.MyPid());或System.Diagnostics.Process.GetCurrentProcess().Kill();)
我关闭应用程序的方法:
private void CloseBtn_Click(object sender, EventArgs e){
FinishAffinity();
JavaSystem.Exit(0);
}
user3480038 answered 2020-08-06T01:12:15Z
0 votes
这些都不适用于Android8。它们都将应用程序保留在后台。我可以通过按关闭所有按钮来证明这一点,并且该应用程序仍然存在。
为了进行测试,我使用了一个全新的简单Android应用程序,并尝试了所有答案。
GaryP answered 2020-08-06T01:12:39Z