Jonas Jonas - 3 months ago 28
C# Question

Xamarin IntentService started by HardwareButton

I tried now for days to get this running, without any success.

I'm trying to react on an hardwarebutton from my handheld via the functions of their specific SDK (it's a Panasonic Toughpad). I'm generating this code in Xamarin and i got one sample of the SDK in Java.

Here's my Code:

First of all the Service itself.
Serviceclass:

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

}
}


Here's the code snipped of the 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>


I'm new in this API connecting things. In my thoughts the Intent should now hear on the
HandheldButtons
via the filter. So when I click the
AppButton
of my Handheld it should start the
IntentServices
Functions?!? Or am I wrong?
In my Opinion they are doing the same in their Java Sample.

Code:

Serviceclass:

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


Just for your information. I also tried to get the event from the
APPBUTTON
with a
BroadcastReceiver
and let it start the
IntentService
without any success. Here are the added Code Snippets:

[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);
}
}


Snippet of AndroidManifest:

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

Answer

I got now a solution for this problem.

You have to check if you have the control of the device buttons. If not, you have to choose the IntentService for the buttons via a menue. After selecting your application the service will work. This simple Check will handle the problems with the 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);
                }