java.lang.SecurityException: without permission an

2019-07-03 00:25发布

问题:

I have this very unpeculiar exception.The thing is have i have this app that has a button to start

 InputMethodService

that starts like this

public class MyGroovyIme extends InputMethodService {

and this is how it looks in manifest.

  <service
        android:name=".MyGroovyIme"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.BIND_INPUT_METHOD" >
        <intent-filter>
            <action android:name="android.view.InputMethod" />
            <!-- <category android:name="android.intent.category.DEFAULT" /> -->
        </intent-filter>

        <meta-data
            android:name="android.view.im"
            android:resource="@xml/method" />
    </service>

and this is how I start service

  void startServer() {
    Log.v(TAG, "Starting service...");
    Intent serviceIntent = new Intent(this, MyGroovyIme.class);
    startService(serviceIntent);
    Log.v(TAG, "Starting service...success!!!");
    // this.finish();
    Log.v(TAG, "finish called...");
}

the whole setup works like a charm when i do it on Tablet(Moto XOOM ,its 3.2) but when I do it on Devices that run on 2.2 and 2.1(i havent tried it on 2.3 and cant use emulator either) this is what i get

 java.lang.SecurityException: Not allowed to start service Intent { cmp=com.spp.ime.demo/.MyGroovyIme } without permission android.permission.BIND_INPUT_METHOD
at android.app.ContextImpl.startService(ContextImpl.java:840)
at android.content.ContextWrapper.startService(ContextWrapper.java:336)
at com.spp.ime.demo.GROOVY_IME_DEMOActivity.startServer(GROOVY_IME_DEMOActivity.java:137)
at com.spp.ime.demo.GROOVY_IME_DEMOActivity.onStartClick(GROOVY_IME_DEMOActivity.java:104)
at com.spp.ime.demo.GROOVY_IME_DEMOActivity.onClick(GROOVY_IME_DEMOActivity.java:67)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)

I do understand the security concerns but shouldnt it be same for all devices or am i missing something specific to versions <3.2

回答1:

After a long search have not found a reliable answer and still have not got the exact reason.. but going through the new features have found hardware features that were added at later versions may have led to this leniency in Android OS. Here is the link describing major updates for Android here



回答2:

// only work for pre-lolipop
<uses-permission 
    android:name="android.permission.BIND_INPUT_METHOD" 
    tools:ignore="ProtectedPermissions" 
    android:protectionLevel="signature" />

<service 
    android:name="SimpleIME" 
    android:permission="android.permission.BIND_INPUT_METHOD" 
    android:protectionLevel="signature">

    <intent-filter>
        <action android:name="android.view.InputMethod" />
    </intent-filter>
    <meta-data 
        android:name="android.view.im" 
        android:resource="@xml/method" />
</service>

<supports-screens 
    android:anyDensity="true" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" />