Kiosk Mode / Screen Pinning in Android wear

2019-07-09 02:53发布

问题:

I am developing an Android wear app for Sony Smartwatch 3 with Android Lollipop and can't seem to find a way implement a working kiosk mode.
I've looked everywhere but nothing seems to work.
Also tried screen pinning but there isn't an option to do it on the watch (manually) and adding startLockStart() to my onCreate doesn't seem do to anything - I can still exit the app.

Any help or confirmation that it is not possible will be greatly appreciated

回答1:

Make sure you run Android L MR1 (Android 5.1). In Sony devices running L MR0 (Android 5.0), the Google implementation of startLockTask had a bug which made it possible to exit the app.

Since this is vanilla Google code, it's very likely it's the same situation on other OEM devices.

Also make sure your app's lockTaskMode is not 1, or 3 if you are not using a device admin application.

Hope this helps.

/Marek (Sony Mobile employee)



回答2:

I have written the blog on Medium to achieve Kiosk mode on Android wear. It works fully on Wear OS 2.X and 90% on Wear OS 1.X. Please check on Medium.

Steps :

1: REORDER_TASKS : reorder your app to top in onPause of the app.

ActivityManager activityManager = (ActivityManager) getApplicationContext()
                .getSystemService(Context.ACTIVITY_SERVICE);

        activityManager.moveTaskToFront(getTaskId(), 0);

2: set categories to activity in manifest :

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    <category android:name="android.intent.category.HOME" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

3: Override button’s functionality

public boolean onKeyDown(int keyCode, KeyEvent event){
    if (event.getRepeatCount() == 0) {
        if (keyCode == KeyEvent.KEYCODE_STEM_1) {
            // Do stuff
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_STEM_2) {
            // Do stuff
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_STEM_3) {
            // Do stuff
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

4: Disable swipe functionality by applying theme

<style name="AppTheme" parent="@android:style/Theme.DeviceDefault">
    <item name="android:windowSwipeToDismiss">false</item>
</style>

5: Relaunch App on Boot

Note :

This functionality is not user friendly and Android Play Store wouldn’t approve of these unless you have a very strong reason