2016-07-25 57 views
3

我現在試了好幾天才得以運行,沒有任何成功。由HardwareButton啓動的Xamarin IntentService

我試圖通過他們特定的SDK(它是松下Toughpad)的功能從我的掌上電腦的硬件按鈕作出反應。我在Xamarin中生成了這段代碼,並且我在Java中獲得了一個SDK的樣本。

這裏是我的代碼:

首先是服務本身。 服務類:

[Service] 
    [IntentFilter(new[] { "com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON" })] 
    class ButtonIntentService : IntentService 
    { 
     public ButtonIntentService() :base("ButtonIntentHandlerThread"){ } 

     protected override void OnHandleIntent(Intent intent) 
     { 
      if (!intent.Action.Equals(AppButtonManager.ActionAppbutton)) 
      { 
       return; 
      } 

      if (ButtonTestFragment.getInstance() !=null) 
      { 
       ButtonTestFragment.getInstance().updateButtonState(intent); 
      } 
      else 
      { 

      } 
     } 

這裏的剪斷的AndroidManifest的代碼。 AndroidManifest:

<service 
     android:enabled="true" 
     android:name="com.SoftwareTestXamarin.Droid.ButtonIntentService" 
     android:label="button api Sample" 
     android:exported="true"> 
    <intent-filter> 
    <action android:name="com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON"/> 
    </intent-filter> 
</service> 

我是新API在連接這些東西。在我的想法中,意圖現在應該通過過濾器在HandheldButtons上聽到。所以當我點擊我的手持設備的AppButton時,它應該啓動IntentServices函數?!?或者我錯了? 在我的意見中,他們在Java示例中也做了同樣的事情。

代碼:

服務類:

public class ButtonIntentService extends IntentService { 

    public ButtonIntentService() { 
     super("Button Intent Handler Thread"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     if (!intent.getAction().equals(AppButtonManager.ACTION_APPBUTTON)) { 
      // Ignore.. 
      return; 
     } 

     if (ButtonTestFragment.getInstance() != null) { 
      ButtonTestFragment.getInstance().updateButtonState(intent); 
     } 

    } 

AndroidManifest:

<service 
      android:name="com.panasonic.toughpad.android.sample.buttons.ButtonIntentService" 
      android:label="@string/lbl_button_service" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON"/> 
      </intent-filter> 
     </service> 

---- UPDATE 04.08 ----

只是供你參考。我也嘗試從APPBUTTON得到BroadcastReceiver的事件,並讓它開始IntentService沒有任何成功。以下是所添加的代碼片段:

[BroadcastReceiver] 
    [IntentFilter(new[] { "com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON" }, Priority = (int)IntentFilterPriority.HighPriority)] 
    class ButtonReceiver : BroadcastReceiver 
    { 
     public override void OnReceive(Context context, Intent intent) 
     { 
      var serviceIntent = new Intent(context, typeof(ButtonIntentService)); 
      serviceIntent.PutExtras(intent); 
      context.StartService(serviceIntent); 
     } 
    } 

片段AndroidManifest的:

<receiver android:name="ButtonReceiver"> 
     <intent-filter> 
      <action android:name="com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON"/> 
     </intent-filter> 
    </receiver> 
+1

一些事情要檢查,因爲我沒有看到它在你的代碼中:1)Xamarin應用程序中給出的適當權限與示例應用程序相比較嗎? (你能鏈接到示例應用程序嗎?) 2)確保可以播放此自定義意圖,然後正確接收。使用SendBroadcast(意圖)在本地活動中測試。 –

回答

0

我現在得到了一個解決這個問題。

你必須檢查是否有設備按鈕的控制。如果沒有,則必須通過菜單爲按鈕選擇IntentService。選擇你的應用程序後,該服務將起作用。 這個簡單的檢查會在處理與IntentService的問題:

if (!AppButtonManager.HasButtonControl) 
       { 
        Intent reconfigureApp = new Intent(Intent.ActionMain); 
        reconfigureApp.AddCategory(Intent.CategoryLauncher); 
        reconfigureApp.SetFlags(ActivityFlags.NewTask); 
        reconfigureApp.SetComponent(new ComponentName("com.panasonic.toughpad.android.service", 
                   "com.panasonic.toughpad.android.appbuttondelegator.ConfigActivity")); 
        activity.StartActivity(reconfigureApp); 
       } 

你不需要的廣播服務使用這個解決方案。 IntentService本身就足夠了。

相關問題