首页 > 解决方案 > Flutter LockTask

问题描述

我想用 android locktask 锁定一个颤动的应用程序。我尝试了几种方法,但 setLockTaskPackages 方法似乎没有做任何事情。一开始我尝试调用MainActivity的onCreate函数中的方法。然后我尝试通过方法通道调用它,但两种方法都不起作用。这是我的带有方法通道的 onCreate 函数:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GeneratedPluginRegistrant.registerWith(this);

        mDevicePolicyManager = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);
        mAdminName = new ComponentName(this, Receiver_Admin.class);

        new MethodChannel(getFlutterView(), "com.packagename/kiosk").setMethodCallHandler(
                new MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, Result result) {
                        mDevicePolicyManager.setLockTaskPackages(
                                mAdminName, 
                                new String[]{"com.packagename"}
                                );
                        result.success(null);
                    }
                }
        );
    }

我的 AndroidMainifest.xml:

        <receiver
                android:name=".receiver.Receiver_Admin"
                android:label="Sehne"
                android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                    android:name="android.app.device_admin"
                    android:resource="@xml/device_admin" />

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

任何想法为什么什么都没有发生?

提前致谢

标签: androidflutter

解决方案


推荐阅读