How to prevent the app from being "minimized" by Appswitch(Recentapps) Xamarin Android

Asked

Viewed 206 times

8

I need to lock the device only for one application.

I use in my project a line that determines that my app becomes a launcher. So, the home screen of the device is my application and the HomeButton ends up always directing to the app. Line below.

[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryHome, Intent.CategoryDefault })]

I was able to lock the buttons to prevent the user from accessing the device settings by changing the files *.kl in the /system/usr/keylayout folder. However, the only way to make this change is with the device rooteaded. I’m looking for ways not to need rooting the device.

The BackButton, I can "lock" on the device within my application, using the public override void OnBackPressed().

The volume buttons I can "lock" on the device using the code below.

    public override bool OnKeyDown(Android.Views.Keycode keyCode, KeyEvent e)
    {
        switch (keyCode)
        {
            case Android.Views.Keycode.VolumeUp:
                Toast.MakeText(this, "Volume Up pressed", ToastLength.Long).Show();
                e.Dispose();
                return true;
            case Android.Views.Keycode.VolumeDown:
                Toast.MakeText(this, "Volume Down pressed", ToastLength.Long).Show();
                e.Dispose();
                OnUserLeaveHint();
                return true;
        }
        return base.OnKeyDown(keyCode, e);
    }

However, the only button that I’m unable to "block" is the AppSwitch, which is the easiest to access the settings.

I tried with the code below, but without success.

protected override void OnPause()
{
    base.OnPause();

    ActivityManager activityManager = (ActivityManager)GetSystemService(Activity.ActivityService);
    activityManager.MoveTaskToFront(TaskId, 0);
}

The taskbar I block too. But only inside the application.

I also read about AccessibilityService, where we can change by creating a Hook on the buttons pressed. But I didn’t get much information and the tests I did were unsuccessful.

Any idea how to block?


I continue with the attempts. This unfortunately is being trial and error. I accessed the Android Management API and set up a company, created a policy and managed to import it into the device. However, there is no way to block device settings (because it is one of the ways to restore it to default settings), which is one of the things required for my application.

I didn’t want to appeal to the . kl files that only work with the rooted device. Does anyone have any idea?

  • I have tried this way which follows - Android Management API: https://developers.google.com/android/management/. But not yet successful

1 answer

3

After much research and conversation with other colleagues, we found that in the method OnPause() you can lock the button AppSwitch(RecentApps) using the code below.

    protected override void OnPause()
    {
        base.OnPause();

        ActivityManager activityManager = (ActivityManager.FromContext(this));
        activityManager.MoveTaskToFront(TaskId, 0);
    }

Some "bugs" may happen if pressed often followed, such as black blinking screen, but only when it is pressed many times in a row.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.