我正在使用 Xamarin Native 应用程序,并且希望在应用程序在 android 中聚焦/不聚焦时执行一些逻辑 - 类似于 Xamarin.Forms Application.OnSleep()
,并且(不要与, 和Application.OnResume()
混淆)Activity.OnResume
Activity.OnPause
只是想知道其他人使用什么解决方案来解决这种情况(除了迁移到 Xamarin.Forms)。
我正在使用 Xamarin Native 应用程序,并且希望在应用程序在 android 中聚焦/不聚焦时执行一些逻辑 - 类似于 Xamarin.Forms Application.OnSleep()
,并且(不要与, 和Application.OnResume()
混淆)Activity.OnResume
Activity.OnPause
只是想知道其他人使用什么解决方案来解决这种情况(除了迁移到 Xamarin.Forms)。
[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 包中。
当应用程序在android中聚焦/失焦时
你的意思是你想监听应用程序在前台还是后台运行?
如果是,您可以使用IActivityLifecycleCallbacks来监听状态。
Application
注册ActivityLifecycleCallbacks
,这样,当应用生命周期中的每个活动发生时,应用程序都可以监听。活动的数量public void onActivityStarted(activity activity)
和数量public void onActivityStopped(activity activity)
可用于确定应用程序是否在前台。因为当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]
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();
}
}