我尝试启动Service时拒绝权限

cod*_*der 4 permissions service android android-input-method android-activity

我正在尝试InputMethodService从一个访问Activity,我遇到了权限问题.这适用于自定义键盘应用.

我想要实现的是将Activity后面创建的文本绑定到InputMethodService.在Activity从打开InputMethodService,然后从Activity我尝试启动Service(可能是这里的问题是我怎么打开.ActivityInputMethodService:

    @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);

}
Run Code Online (Sandbox Code Playgroud)

这是我尝试与以下内容进行沟通InputMethodService的地方Activity:

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

    startService(new Intent(this, MyService.class));
}
Run Code Online (Sandbox Code Playgroud)

这是我的清单文件:

<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>
Run Code Online (Sandbox Code Playgroud)

这是我的堆栈跟踪:

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
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

hac*_*bod 6

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

这在"安全性"部分中有所描述:http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

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

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

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


aho*_*der 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>
Run Code Online (Sandbox Code Playgroud)

你说错了.