如何从键盘列表中打开一个activity?

How to open an activity from the keyboard list?

我正在开发自定义键盘,想添加一个首选项页面。

我注意到 gboard 能够从 android 设置中的虚拟键盘列表中打开其首选项活动。

我也试过这样做,但我找不到任何关于如何做的信息。我试图搜索意图,但没有找到。

你有什么想法吗?

编辑:这是我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yvo.mockingkeyboard">
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name="RootPreferences" android:label="@string/settings_name" android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
            </intent-filter>
        </activity>

        <service
            android:name="MockingKeyboard"
            android:label="Mocking Keyboard"
            android:permission="android.permission.BIND_INPUT_METHOD"
            >
            <meta-data android:name="android.view.im" android:resource="@xml/method"/>
            <intent-filter>
                <action android:name="android.view.InputMethod"/>
            </intent-filter>
        </service>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.yvo.mockingkeyboard.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
    </application>

</manifest>

您是否在应用程序列表中获取软件包名称?

如果没有,您可以使用此键盘应用程序列表代码

 InputMethodManager imeManager =
            (InputMethodManager) getApplicationContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);

 //Keyboard application packages  
 List<InputMethodInfo> inputMethods = imeManager.getInputMethodList();
 for (int i = 0; i < inputMethods.size(); i++) {
        InputMethodInfo im = inputMethods.get(i);
        String packageName = im.getPackageName();

        //Getting package info
        try {
            PackageInfo pi = getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);

            String appname = pi.applicationInfo.loadLabel(getPackageManager()).toString();
            String pname = pi.packageName;

            String versionName = pi.versionName;
            int versionCode = pi.versionCode;
            Drawable icon = pi.applicationInfo.loadIcon(getPackageManager());

            Log.e("InputMethods", "appname: "+appname);
            Log.e("InputMethods", "pname: "+pname);
            Log.e("InputMethods", "versionName: "+versionName);
            Log.e("InputMethods", "versionCode: "+versionCode);
            Log.e("InputMethods", "icon: "+icon);

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
 }

按包名启动应用程序..

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("package_name");
//null pointer check in case package name was not found
if (launchIntent != null) {
   startActivity(launchIntent);
}

我发现设置显示缺少什么。
您需要添加属性

android:settingsActivity="com.example.package.PreferencesActivity"

在您的 method.xml 标签内

<input-method>