【问题标题】:How can we make app in kiosk mode using xamarin?我们如何使用 xamarin 在信息亭模式下制作应用程序?
【发布时间】:2017-10-09 01:59:07
【问题描述】:

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

【问题讨论】:

  • 如果您遇到错误,请告诉我们具体的错误信息是什么。
  • 你检查我的答案了吗?有什么问题吗?
  • 我也在尝试实现相同的功能,但对 App 类感到震惊,请您指导我在便携式设备中实现 App 类。

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


【解决方案1】:

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

由于您没有发布代码和错误消息,我们不知道发生了什么,我只是尝试按照您发布的博客创建这样一个示例,它在我身边运行良好。

这里是服务:

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

我在MainActivityOnCreate 中启动了这项服务:

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

【讨论】:

    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 2011-01-11
    • 2020-05-18
    • 1970-01-01
    • 2018-03-06
    • 2021-02-14
    相关资源
    最近更新 更多