Xamarin Native 相当于 Xamarin.Forms App OnSleep 和 OnResume

Xamarin Native equivalent of Xamarin.Forms App OnSleep and OnResume

我正在使用 Xamarin Native 应用程序,并希望在应用程序聚焦/不聚焦时执行一些逻辑 android - 类似于 Xamarin.Forms Application.OnSleep()Application.OnResume()(不要与 Activity.OnResumeActivity.OnPause 混淆)

只是想知道其他人使用什么解决方案来解决这种情况(除了迁移到 Xamarin.Forms)。

when the application is focused / out of focused in android

你是说你想监听应用程序 运行 在前台还是后台?

如果是,您可以使用 IActivityLifecycleCallbacks 来监听状态。

Application注册ActivityLifecycleCallbacks,这样,当app生命周期中的每个activity发生时,Application都可以被监听。一个activity的public void onActivityStarted(activity activity)public void onActivityStopped(activity activity)的个数可以判断app是否在前台。因为当app在前台的时候,肯定有一个activity启动了onActivityStarted而没有启动onActivityStopped,所以统计app打开的activity数肯定是1。当应用程序切换到后台时,activityStartCount 将为 0

所以写一个 Helper 类 :

public class AppFrontBackHelper
{

    public static OnAppStatusListener mOnAppStatusListener;
    private LifecycleCallBack lifecycleCallBack;
    public AppFrontBackHelper()
    {

    }
    /**
     * Register status listener, only used in Application
     * @param application
     * @param listener
     */
    public void register(Application application, OnAppStatusListener listener)
    {
        mOnAppStatusListener = listener;
        lifecycleCallBack = new LifecycleCallBack();
        application.RegisterActivityLifecycleCallbacks(lifecycleCallBack);
    }

    public void unRegister(Application application) => application.UnregisterActivityLifecycleCallbacks(lifecycleCallBack);

    public interface OnAppStatusListener
    {
        void onFront();
        void onBack();
    }
    public class LifecycleCallBack : Java.Lang.Object, Application.IActivityLifecycleCallbacks
    {

        public int activityStartCount { get; private set; }

        public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
        {
        }

        public void OnActivityDestroyed(Activity activity)
        {
        }

        public void OnActivityPaused(Activity activity)
        {
        }

        public void OnActivityResumed(Activity activity)
        {
        }

        public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
        {
        }

        public void OnActivityStarted(Activity activity)
        {
            activityStartCount++;
            //A value from 1 to 0 indicates cutting from the background to the foreground
            if (activityStartCount == 1)
            {

                if (mOnAppStatusListener != null)
                {
                    mOnAppStatusListener.onFront();
                }
            }
        }

        public void OnActivityStopped(Activity activity)
        {
            activityStartCount--;
            //A value from 1 to 0 indicates cutting from the foreground to the background
            if (activityStartCount == 0)
            {
                //from foreground to background
                if (mOnAppStatusListener != null)
                {
                    mOnAppStatusListener.onBack();
                }
            }
        }
    }

}

然后自定义一个 Application 并注册监听器:

[Application]
public class MyApplication : Application,AppFrontBackHelper.OnAppStatusListener
{
    protected MyApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }
    public override void OnCreate()
    {
        base.OnCreate();
        AppFrontBackHelper appFrontBackHelper = new AppFrontBackHelper();
        appFrontBackHelper.register(this, this);
    }
    public void onBack()
    {
        Toast.MakeText(this, "from front to back", ToastLength.Short).Show();
    }



    public void onFront()
    {
        Toast.MakeText(this, "from back to front", ToastLength.Short).Show();
    }


}
[Application]
public class MyApp : Application, ILifecycleObserver
{
    [Export, Lifecycle.Event.OnStop]
    public void OnAppBackgrounded()
    {
    }

    [Export, Lifecycle.Event.OnStart]
    public void OnAppForegrounded()
    {
    }

    public override void OnCreate()
    {
        // For handling when the app goes into the foreground or background
        ProcessLifecycleOwner.Get().Lifecycle.AddObserver(this);
    }

到目前为止,它似乎按预期工作。 ProcessLifecycleOwner 在 Xamarin.Android.Arch.Lifecycle.Extensions nuget 包中。