4

I want to open my application immediately when S Pen is detached , How can you do this, if the methods put under onSPenDetached is only called when my application is opened again?

Thanks, Chandu

jam
  • 3,640
  • 5
  • 34
  • 50
Chandu
  • 167
  • 1
  • 5
  • 12

2 Answers2

5

The following works on my Galaxy Tab A 9.7 with S-Pen (SM-P550) running Android 5.0.2.

Attaching and detaching the stylus creates Broadcast Intents of type com.samsung.pen.INSERT with a booleanExtra named penInsert of false if detached and true if put back into the device.

Thus a Broadcast Receiver can be created that filters this kind of events. The following code is for such a Broadcast Receiver which starts OneNote if the stylus is detached:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class SPenDetachIntentBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent penInsertIntent) {
        if (!penInsertIntent.getBooleanExtra("penInsert", true)) {
            try {
                Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.microsoft.office.onenote");
                context.startActivity(launchIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

In the Manifest file you need to declare it as a receiver listening for com.samsung.pen.INSERT Broadcast Intents with an intent filter. The following entry in a project's AndroidManifest.xml declares SPenDetachBroadcastReceiver, generates an instance and makes it listening for com.samsung.pen.Insert Broadcast Intents:

<receiver
            android:name=".SPenDetachIntentBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.samsung.pen.INSERT" />
            </intent-filter>
</receiver>

The advantage over using registerSPenDetachmentListener on an SPenEventLibrary object to register a Service with an onSPenDetached method implemented is that you do not need any additional library files and you also do not need additional permissions.

tastaturtier
  • 126
  • 1
  • 5
1

You will need to create a BroadcastReceiver and a Service.

The service:

public class SPenService extends Service {

    SPenEventLibrary mSPenEventLibrary = new SPenEventLibrary();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mSPenEventLibrary.registerSPenDetachmentListener(this, new SPenDetachmentListener() {

            @Override
            public void onSPenDetached(boolean bDetached) {
                if (bDetached) {
                    Toast.makeText(SPenService.this, "S Pen Detached", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(SPenService.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } else {
                    Toast.makeText(SPenService.this, "S Pen Inserted", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSPenEventLibrary.unregisterSPenDetachmentListener(this);
    }

}

The receiver:

public class SPenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        String action = intent.getAction();

        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            context.startService(new Intent(context, SPenService.class));
        }
    }

}

The manifest (inside the <application> tag):

<receiver android:name=".SPenReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service android:name=".SPenService" >
</service>
tomrozb
  • 25,773
  • 31
  • 101
  • 122