当我尝试启动服务时权限被拒绝

发布于 2024-12-16 20:57:54 字数 4371 浏览 5 评论 0原文

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

我想要实现的是将 Activity 中创建的文本绑定回 InputMethodService 中。 ActivityInputMethodService 打开,然后从 Activity 中,我尝试启动 Service(这可能这是我如何从 InputMethodService 打开 Activity 的方法:

    @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 进行通信的地方。 >活动:

    @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

有什么想法吗?

I am trying to access an InputMethodService from an Activity, and I am running into issues with the permissions. This is for a custom keyboard app.

What I am trying to achieve is to bind the text, which is created in the Activity back into the InputMethodService. The Activity is opened from the InputMethodService, then from the Activity, I try to start the Service(which may be the issue. Here is how I open the Activity from the 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);

}

Here is where I try to communicate with the InputMethodService from the Activity:

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

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

Here is my Manifest File:

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

and here is my stack trace:

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怎言笑 2024-12-23 20:57:55

您需要添加权限。

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

你设置错了。

You need to add the permission.

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

You are setting it wrong.

策马西风 2024-12-23 20:57:55

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

Another suggestion: You need both the android:permission in the service and the uses-permission in the application - outside the service - at the same time

初见终念 2024-12-23 20:57:55

在清单中:
第一:删除 my 和 package 之间的点
接下来 - 如果第一个没有帮助:在服务名称前面加一个点:

In manifest:
First: remove the dot btw my and package
Next - if First doesn't help: put a dot in front of service name:

生来就爱笑 2024-12-23 20:57:54

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

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

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

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

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

You can't do this. The platform requires that input method services require the BIND_INPUT_METHOD permission, and no third party applications can get that permission. This is an important security mechanism to ensure that only the platform itself can interact with an input method service, and no applications can spoof the platform while the user is interacting with the input method.

This is described in the "Security" section here: http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

If this is some other app's input method service, that is the end of the story, the only way to interact with it is through the formal IME architecture of the platform.

If this is your own app's input method service, there are many tricks you can use to interact with it since you are running in the same process as it. The easiest is just to have it set a global variable of the service object when it is created, which you can access from elsewhere in your app.

If you really need to actually put the service in the started state... well, you can't do that, because that is not how input methods work. You will need to make a second service that you start and coordinate between the two services. Again, these should all be running in the same process, so you can take advantage of that to directly call between them to do whatever interactions you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文