简体   繁体   English

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

[英]How to open an activity from the keyboard list?

I'm developping a custom keyboard, and want to add a preference page.我正在开发一个自定义键盘,并想添加一个首选项页面。

I noticed that gboard was able to open its preference acivity from the virtual keyboard list in the android settings.我注意到 gboard 能够从 android 设置中的虚拟键盘列表中打开它的偏好活动。

安卓键盘列表

I tried to do the same, but I can't find any information on how to do this.我试图做同样的事情,但我找不到任何关于如何做到这一点的信息。 I tried to search for an intent, but failed to find any.我试图搜索一个意图,但没有找到任何意图。

Do you have an idea ?你有想法吗 ?

Edit : Here is my manifest编辑:这是我的清单

<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>

I found what was lacking for the settings to show up.我发现设置显示缺少什么。
You need to add the attribute您需要添加属性

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

In your method.xml, inside the tag在你的 method.xml 中,标签内

<input-method>

Are you getting package name with your application list?您是否在应用程序列表中获取包名称?

If no, you can use this Keyboard Application list code如果没有,您可以使用此键盘应用程序列表代码

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

Launch application by package name..按包名启动应用程序..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM