How can we make app in kiosk mode using xamarin?

2019-09-20 18:43发布

问题:

I'm creating new app using xamarin. I have already completed some part using some sample codes. I'm able to disable back buttons, volume buttons and power button. But when trying to disable home button I'm getting error on debugging. I'm following this link,Kiosk mode in Andriod.

回答1:

But when trying to disable home button I'm getting error on debugging.

Since you didn't post your code and your error message, we don't know what happened, I just tried to create such a sample followed the blog your posted and it works fine by my side.

Here is the service:

namespace KioskModeAndroid
{
    [Service]
    [IntentFilter(new[] { "KioskModeAndroid.KioskService" })]
    public class KioskService : Service
    {
        private static long INTERVAL = Java.Util.Concurrent.TimeUnit.Seconds.ToMillis(2);
        private static string TAG = typeof(KioskService).Name;
        private static string PREF_KIOSK_MODE = "pref_kiosk_mode";

        private Thread t = null;
        private Context ctx = null;
        private bool running = false;

        public override void OnDestroy()
        {
            Log.Info(TAG, "Stopping service 'KioskService'");
            running = false;
            base.OnDestroy();
        }

        [return: GeneratedEnum]
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            Log.Info(TAG, "Starting service 'KioskService'");
            running = true;
            ctx = this;

            t = new Thread(() =>
            {
                while (running)
                {
                    handleKioskMode();
                    Thread.Sleep(INTERVAL);
                }
                StopSelf();
            });
            t.Start();

            return StartCommandResult.NotSticky;
        }

        private void handleKioskMode()
        {
            if (isKioskModeActive(ctx))
            {
            }
            if (isInBackground())
            {
                restoreApp();
            }
        }

        private bool isKioskModeActive(Context context)
        {
            var sp = PreferenceManager.GetDefaultSharedPreferences(context);
            return sp.GetBoolean(PREF_KIOSK_MODE, false);
        }

        private bool isInBackground()
        {
            var am = ctx.GetSystemService(Context.ActivityService) as ActivityManager;
            var processes = am.RunningAppProcesses;
            foreach (var process in processes)
            {
                if (process.Importance == ActivityManager.RunningAppProcessInfo.ImportanceForeground)
                {
                    foreach (var activeprocess in process.PkgList)
                    {
                        if (activeprocess == ctx.PackageName)
                            return false;
                    }
                }
            }
            return true;
        }

        private void restoreApp()
        {
            Intent i = new Intent(ctx, typeof(MainActivity));
            i.AddFlags(ActivityFlags.NewTask);
            ctx.StartActivity(i);
        }

        public override IBinder OnBind(Intent intent)
        {
            return null;
        }
    }
}

I started this service in the OnCreate of MainActivity:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);
    StartService(new Intent(this, typeof(KioskService)));
}