2016-07-25 135 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本身就足够了。