Xamarin C# activity on screen (un)lock event

2019-08-29 12:03发布

问题:

Just wondering if there is an event In Xamarin for the activity class like OnStart, OnResume, OnPause etc. that gets fired when the user locks or unlocks their screen while the app is open, I've been looking for such an event in documentation etc, but I haven't been able to find it.

If there is no such event, then how can we create it?

回答1:

Xamarin C# activity on screen (un)lock event

you could create a listener class to listen for android lock screen and unlock events,use broadcast to receive the state of screen.

first create a ScreenListener.cs :

public class ScreenListener
{
    private Context mContext;
    private ScreenBroadcastReceiver mScreenReceiver;
    private static ScreenStateListener mScreenStateListener;

    public ScreenListener(Context context)
    {
        mContext = context;
        mScreenReceiver = new ScreenBroadcastReceiver();
    }

    /**
     * screen BroadcastReceiver
     */
    private class ScreenBroadcastReceiver : BroadcastReceiver
    {
    private String action = null;

        public override void OnReceive(Context context, Intent intent)
        {
            action = intent.Action;
            if (Intent.ActionScreenOn == action)
            { // screen on
                mScreenStateListener.onScreenOn();
            }
            else if (Intent.ActionScreenOff == action)
            { // screen off
                mScreenStateListener.onScreenOff();
            }
            else if (Intent.ActionUserPresent == action)
            { // unlock
                mScreenStateListener.onUserPresent();
            }
        }
    }

/**
 * begin to listen screen state
 *
 * @param listener
 */
public void begin(ScreenStateListener listener)
{
    mScreenStateListener = listener;
    registerListener();
    getScreenState();
}

/**
 * get screen state
 */
private void getScreenState()
{
    PowerManager manager = (PowerManager)mContext
            .GetSystemService(Context.PowerService);
    if (manager.IsScreenOn)
    {
        if (mScreenStateListener != null)
        {
            mScreenStateListener.onScreenOn();
        }
    }
    else
    {
        if (mScreenStateListener != null)
        {
            mScreenStateListener.onScreenOff();
        }
    }
}

/**
 * stop listen screen state
 */
public void unregisterListener()
{
    mContext.UnregisterReceiver(mScreenReceiver);
}

/**
 * regist screen state broadcast
 */
private void registerListener()
{
    IntentFilter filter = new IntentFilter();
    filter.AddAction(Intent.ActionScreenOn);
    filter.AddAction(Intent.ActionScreenOff);
    filter.AddAction(Intent.ActionUserPresent);
    mContext.RegisterReceiver(mScreenReceiver, filter);
}

public interface ScreenStateListener
{// Returns screen status information to the caller
    void onScreenOn();

    void onScreenOff();

    void onUserPresent();
}
}

then in the MainActivity.cs:

public class MainActivity : AppCompatActivity,ScreenStateListener
{
   ScreenListener mScreenListener;
   protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        mScreenListener = new ScreenListener(this);
    }
   protected override void OnDestroy()
    {
        base.OnDestroy();
        mScreenListener.unregisterListener();
    }

   protected override void OnResume()
    {
        base.OnResume();
        mScreenListener.begin(this);

    }
   public void onScreenOn()
    {
        Console.WriteLine("onScreenOn");
    }

   public void onScreenOff()
    {
        Console.WriteLine("onScreenOff");
    }

   public void onUserPresent()
    {
        Console.WriteLine("onUserPresent");
    }


}


回答2:

Here's a sample of Xamarin.Android activities lifecycle.

You can override any of the lifecycle methods like this:

protected override void OnResume() {
    base.OnResume(); 
}

Like in native Android OnPause is fired when the system is about to put the activity into the background. OnResume is called when the activity is ready to start interacting with the user.

OnPause and OnResume can be used for your (un)lock events.