S笔在Android笔记中分离时,将您的应用程序带到前面?

时间:2013-02-20 09:20:54

标签: android galaxy

我希望在分离S Pen时立即打开我的应用程序,如果仅在我的应用程序再次打开时调用onSPenDetached下的方法,你怎么能这样做?

谢谢, Chandu

2 个答案:

答案 0 :(得分:4)

以下版本适用于运行Android 5.0.2的带有S-Pen(SM-P550)的Galaxy Tab A 9.7。

附加和分离手写笔会创建类型为com.samsung.pen.INSERT的广播意图,如果已分离,则为penInsert false的booleanExtra,如果放回设备,则为true

因此,可以创建过滤此类事件的广播接收器。以下代码适用于这样的广播接收器,如果分离了触控笔,则启动OneNote:

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

在Manifest文件中,您需要将其声明为接收方,使用intent过滤器监听com.samsung.pen.INSERT Broadcast Intents。项目AndroidManifest.xml中的以下条目声明SPenDetachBroadcastReceiver,生成一个实例并使其监听com.samsung.pen.Insert广播意图:

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

registerSPenDetachmentListener对象上使用SPenEventLibrary注册Service并实现onSPenDetached方法的优势在于,您不需要任何其他库文件,而且还需要不需要额外的权限。

答案 1 :(得分:1)

您需要创建BroadcastReceiverService

服务:

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

}

接收者:

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

}

清单(<application>标记内):

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

<service android:name=".SPenService" >
</service>