我们如何使用xamarin在kiosk模式下制作应用程序?

时间:2017-05-10 12:11:34

标签: xamarin xamarin.ios xamarin.android xamarin.forms

我正在使用xamarin创建新应用。我已经使用一些示例代码完成了一些部分。我可以禁用后退按钮,音量按钮和电源按钮。 但是当试图禁用主页按钮时,我在调试时遇到错误。 我关注此链接Kiosk mode in Andriod

1 个答案:

答案 0 :(得分:0)

  

但是当尝试禁用主页按钮时,我在调试时遇到错误。

由于您没有发布您的代码和错误消息,我们不知道发生了什么,我只是尝试按照您发布的博客创建这样的示例,并且它在我身边正常工作。< / p>

这是服务:

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;
        }
    }
}

我在OnCreate的{​​{1}}中启动了此服务:

MainActivity