2017-05-10 55 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))); 
} 
相關問題