Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由HardwareButton启动的Xamarin IntentService_C#_Android_Service_Xamarin_Intentservice - Fatal编程技术网

C# 由HardwareButton启动的Xamarin IntentService

C# 由HardwareButton启动的Xamarin IntentService,c#,android,service,xamarin,intentservice,C#,Android,Service,Xamarin,Intentservice,我现在试了几天来让它运行,但没有成功 我正试图通过他们特定SDK(松下Toughpad)的功能对我的手持设备上的硬件按钮做出反应。我在Xamarin中生成这段代码,我得到了Java中SDK的一个示例 这是我的密码: 首先是服务本身。 服务级别: [Service] [IntentFilter(new[] { "com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON" } )] class ButtonIntent

我现在试了几天来让它运行,但没有成功

我正试图通过他们特定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
            {

            }
        }
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截取的代码。 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>
<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>
<receiver android:name="ButtonReceiver">
        <intent-filter>
            <action android:name="com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON"/>
        </intent-filter>
    </receiver>
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>
<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>
<receiver android:name="ButtonReceiver">
        <intent-filter>
            <action android:name="com.panasonic.toughpad.android.api.appbutton.intent.APPBUTTON"/>
        </intent-filter>
    </receiver>
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>
<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>
<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本身就足够了。

有几件事需要检查,因为我在您的代码中没有看到它:1)与示例应用程序相比,您的Xamarin应用程序中授予的权限是否正确?(你也可以链接到示例应用程序吗?)2)确保可以广播并正确接收此自定义意图。使用
SendBroadcast(Intent)
在您的活动中进行本地测试。