2017-05-10 58 views
-1

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

+1

如果你得到一个错误,那么请告诉我们具体的错误信息是什么。 – Jason

+0

你检查了我的答案吗?任何问题? –

回答

0

但是,当试图禁用主页按钮时,我得到调试错误。

由于您没有发布您的代码和您的错误消息,我们不知道发生了什么,我只是尝试创建这样一个示例,跟着您发布的博客,它在我身边正常工作。

这里的服务:

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))); 
} 
相关问题