4

我正在尝试从 访问InputMethodService,但我遇到Activity了权限问题。这是用于自定义键盘应用程序。

我想要实现的是将Activity后面创建的文本绑定到InputMethodService. 是从Activity中打开的InputMethodService,然后从 中Activity,我尝试启动Service(这可能是问题所在。这是我Activity从 中打开的方式InputMethodService

    @Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
    super.onStartInputView(attribute, restarting);

    Intent intent = new Intent(this, MyKeyboard.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    context.startActivity(intent);

}

这是我尝试与InputMethodService来自的沟通的地方Activity

    @Override
public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    setContentView(R.xml.keyboard);

    startService(new Intent(this, MyService.class));
}

这是我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.my.package">
<application android:label="@string/ime_name">
    <service android:name="MyService"
            android:permission="android.permission.BIND_INPUT_METHOD">
        <intent-filter>
            <action android:name="android.view.InputMethod" />
        </intent-filter>
        <meta-data android:name="android.view.im" android:resource="@xml/method" />
    </service>
    <activity android:name=".MyKeyboard" android:theme="@style/Theme.Transparent">
    </activity>
</application>

这是我的堆栈跟踪:

11-18 15:58:34.732: E/AndroidRuntime(5458): Uncaught handler: thread main exiting due to uncaught exception
11-18 15:58:34.752: E/AndroidRuntime(5458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.MyActivity}: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.mypackage/.MyService} without permission android.permission.BIND_INPUT_METHOD
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.os.Looper.loop(Looper.java:123)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at  java.lang.reflect.Method.invokeNative(Native Method)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at java.lang.reflect.Method.invoke(Method.java:521)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at dalvik.system.NativeStart.main(Native Method)
11-18 15:58:34.752: E/AndroidRuntime(5458): Caused by: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.mypackage/.MyService } without permission android.permission.BIND_INPUT_METHOD
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.ApplicationContext.startService(ApplicationContext.java:765)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.content.ContextWrapper.startService(ContextWrapper.java:326)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at com.mypackage.MyActivity.onCreate(MyActivity.java:94)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-18 15:58:34.752: E/AndroidRuntime(5458):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-18 15:58:34.752: E/AndroidRuntime(5458):     ... 11 more

有任何想法吗?

4

4 回答 4

6

你不能这样做。平台要求输入法服务需要 BIND_INPUT_METHOD 权限,第三方应用无法获得该权限。这是一个重要的安全机制,可以确保只有平台本身可以与输入法服务进行交互,而在用户与输入法交互时,任何应用程序都不能欺骗平台。

这在此处的“安全”部分中进行了描述:http: //developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

如果这是其他应用程序的输入法服务,那就是故事的结尾,与之交互的唯一方法是通过平台的正式 IME 架构。

如果这是您自己的应用程序的输入法服务,您可以使用许多技巧与它进行交互,因为您与它在同一进程中运行。最简单的方法是在创建服务对象时设置它的全局变量,您可以从应用程序的其他位置访问它。

如果您真的需要将服务置于启动状态……好吧,您不能这样做,因为输入法不是这样工作的。您将需要创建第二个服务来启动并在这两个服务之间进行协调。同样,这些都应该在同一个进程中运行,因此您可以利用它在它们之间直接调用以执行您想要的任何交互。

于 2011-11-28T03:36:50.743 回答
5

您需要添加权限。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.my.package">
<application android:label="@string/ime_name">
    <service android:name="MyService">
        <intent-filter>
            <action android:name="android.view.InputMethod" />
        </intent-filter>
        <meta-data android:name="android.view.im" android:resource="@xml/method" />
    </service>
    <activity android:name=".MyKeyboard" android:theme="@style/Theme.Transparent">
    </activity>

    <uses-permission android:name="android.permission.BIND_INPUT_METHOD"/>
</application>

你设置错了。

于 2011-11-18T21:45:54.570 回答
1

另一个建议:您同时需要服务中的 android:permission 和应用程序中的 uses-permission - 在服务之外 - 同时

于 2011-11-23T23:39:14.257 回答
-1

在清单中: 首先:删除点 btw my 和 package Next - 如果 First 没有帮助:在服务名称前面放一个点:

于 2011-11-23T15:57:58.367 回答