我们如何使用Xamarin在信息亭模式下制作应用程序? [英] How can we make app in kiosk mode using xamarin?

查看:87
本文介绍了我们如何使用Xamarin在信息亭模式下制作应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xamarin创建新的应用程序.我已经使用一些示例代码完成了一部分.我可以禁用后退按钮,音量按钮和电源按钮. 但是,当尝试禁用主页按钮时,调试时出现错误. 我正在跟踪此链接,

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.

推荐答案

但是当尝试禁用主页按钮时,调试时出现错误.

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.

这是服务:

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中启动了此服务:

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

这篇关于我们如何使用Xamarin在信息亭模式下制作应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆