我现在试了好几天才得以运行,没有任何成功。由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)Xamarin应用程序中给出的适当权限与示例应用程序相比较吗? (你能链接到示例应用程序吗?) 2)确保可以播放此自定义意图,然后正确接收。使用SendBroadcast(意图)在本地活动中测试。 –