Permission denied when trying to start a service

I am trying to access InputMethodService from an Activity and am having permission problems. This is for a custom keyboard application.

I am trying to link the text that is created in the Activity back to the InputMethodService . Activity opens from InputMethodService , then from Activity , I try to start Service (which may be a problem. Here is how I open Activity from 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 I am trying to contact InputMethodService with 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?

+4
source share
4 answers

You cannot do this. The platform requires input method services to require BIND_INPUT_METHOD permission, and no third-party applications can get this permission. This is an important security mechanism to ensure that only the platform itself can interact with the input method service, and no application can replace the platform while the user interacts 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 application 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 application input method, there are many tricks that you can use to interact with it, since you are working in the same process as it is. The simplest thing is to simply set the global variable of the service object when it is created, which you can access from elsewhere in your application.

If you really need to actually put the service in its initial state ... well, you cannot do this because that is not how the input methods work. You will need to do a second service that you will start and coordinate between the two services. Again, they should all work in the same process, so you can use this to directly call between them, to make any interactions that you want.

+6
source

You need to add 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 mistaken.

+5
source

Another suggestion: you need both android: permission in the service, and use-permission in the application - outside the service - at the same time

+1
source

In the manifest: First: remove the btw my and package point. Next - if First does not help: put a dot in front of the service name:

-1
source

Source: https://habr.com/ru/post/1381973/


All Articles