gpt4 book ai didi

c# - Xamarin IntentService 由 HardwareButton 启动

转载 作者:太空狗 更新时间:2023-10-29 14:47:17 25 4
gpt4 key购买 nike

我现在尝试了好几天让这个运行起来,但没有成功。

我正在尝试通过其特定 SDK(它是 Panasonic Toughpad)的功能对手持设备上的硬件按钮使用react。我在 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 的代码 fragment 。安卓 list :

    <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 的新手。在我看来,Intent 现在应该通过过滤器在 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);
}

}

安卓 list :

<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----

仅供引用。我还尝试使用 BroadcastReceiverAPPBUTTON 获取事件并让它启动 IntentService 但没有成功。以下是添加的代码 fragment :

[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 的 fragment :

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

最佳答案

我现在找到了这个问题的解决方案。

您必须检查您是否可以控制设备按钮。如果没有,您必须通过菜单为按钮选择 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 本身就足够了。

关于c# - Xamarin IntentService 由 HardwareButton 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38561408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com