当我尝试startService权限被拒绝 [英] permission denied when I try to startService

查看:167
本文介绍了当我尝试startService权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从活动访问 InputMethodService ,和我遇到的权限问题。这是一个自定义键盘应用程序。

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.

我所试图实现的是文本,这是在活动创建绑定 InputMethodService 。该活动 InputMethodService ,然后从活动,我尝试启动服务(这可能是问题。这里是我打开活动 InputMethodService

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

}

下面是我尝试与沟通的 InputMethodService 活动

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

下面是我的清单文件:

<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

任何想法?

推荐答案

您无法做到这一点。该平台要求的输入法服务要求BIND_INPUT_METHOD许可,也没有第三方应用程序可以得到许可。这是一个重要的安全机制,以确保只有平台本身可以与输入法服务进行交互,并且没有应用程序可以欺骗平台在用户与输入法进行交互。

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.

这是安全这里一节中描述:<一href="http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html">http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

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

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

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.

这篇关于当我尝试startService权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆