Xamarin在后退按钮上关闭Android应用程序

时间:2015-05-16 09:31:41

标签: c# android xamarin

我尝试了3种不同的方法

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    System.Environment.Exit(0);

public override void OnBackPressed()
{
    Finish();
}

public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
    if (keyCode == Keycode.Back)
    {
            System.Environment.Exit(0);
            return true;
    }
    return base.OnKeyDown(keyCode, e);
}

上述所有内容似乎都无法正常工作

8 个答案:

答案 0 :(得分:19)

按下物理后退按钮时,您可以使用DepedencyService for closing an app

在您的用户界面(PCL)中,执行以下操作:

protected override bool OnBackButtonPressed()
{
   if (Device.OS == TargetPlatform.Android)
       DependencyService.Get<IAndroidMethods>().CloseApp();

   return base.OnBackButtonPressed();
}

还创建一个界面(在您的UI PCL中):

public interface IAndroidMethods
{
    void CloseApp();
}

现在在Android项目中实现特定于Android的逻辑:

[assembly: Xamarin.Forms.Dependency(typeof(AndroidMethods))]
namespace Your.Namespace
{
   public class AndroidMethods : IAndroidMethods
   {
       public void CloseApp()
       {
            Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
       }
   }
}

答案 1 :(得分:3)

如果你想退出App而不杀人并回到主屏幕,那么如果你想从它关闭的地方恢复它。您可以在相关活动中按照以下方式实施。

  public override void OnBackPressed()
    {
        Intent startMain = new Intent(Intent.ActionMain);
        startMain.AddCategory(Intent.CategoryHome);
        startMain.SetFlags(ActivityFlags.NewTask);
        StartActivity(startMain);

    }

希望这有帮助。

答案 2 :(得分:1)

我尝试了这段代码,它在我这边工作。希望这段代码对您有所帮助。

protected override bool OnBackButtonPressed()
    {
        Device.BeginInvokeOnMainThread(async () =>
        {
            var result = await DisplayAlert("Alert!", "Do you really want to exit the application?", "Yes", "No");
            if (result)
            {
                if (Device.OS == TargetPlatform.Android)
                {
                    Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
                }
            }
        });
        return true;
    }

答案 3 :(得分:0)

public override void OnBackPressed()
{
    Finish();
    Android.OS.Process.KillProcess (Android.OS.Process.MyPid ());
}

答案 4 :(得分:0)

在您的活动中尝试此操作:

this.FinishAffinity();
祝你好运!!

答案 5 :(得分:0)

Finish(); 

为我工作。从主要活动开始执行。

答案 6 :(得分:0)

您还可以要求用户确认退出应用程序:

public override void OnBackPressed()
{
    RunOnUiThread(
        async () =>
        {
            var isCloseApp = await AlertAsync(this, "NameOfApp", "Do you want to close this app?", "Yes", "No");

            if (isCloseApp)
            {
                var activity = (Activity)Forms.Context;
                activity.FinishAffinity();
            }
        });
}

public Task<bool> AlertAsync(Context context, string title, string message, string positiveButton, string negativeButton)
{
    var tcs = new TaskCompletionSource<bool>();

    using (var db = new AlertDialog.Builder(context))
    {
        db.SetTitle(title);
        db.SetMessage(message);
        db.SetPositiveButton(positiveButton, (sender, args) => { tcs.TrySetResult(true); });
        db.SetNegativeButton(negativeButton, (sender, args) => { tcs.TrySetResult(false); });
        db.Show();
    }

    return tcs.Task;
}

Xamarin.Android await AlertDialog.Builder

答案 7 :(得分:0)

尝试以下解决方案:

protected override bool OnBackButtonPressed()
{           
    Device.BeginInvokeOnMainThread(async () => 
    {
        var res = await this.DisplayAlert("Do you really want to exit the application?", "","Yes", "No").ConfigureAwait(false);

        if (res) System.Diagnostics.Process.GetCurrentProcess().CloseMainWindow();
    });           
    return true;
}