我現在試了好幾天才得以運行,沒有任何成功。由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(意圖)在本地活動中測試。 –