如何在android中使用Broadcastreceiver或Intent启动InputMethodService?

时间:2011-09-29 05:43:27

标签: android broadcastreceiver android-service

我们现在有一个'InputMethodService'作为一个独立的应用程序,我们想要为另一个应用程序中的特定活动启动此InputMethodService。 我们尝试使用广播接收器,但它给出了以下错误:

Intent myService = new Intent(context, Cs_key.class);
context.startService(myService);

LOGCATE:

-

29 11:06:51.624: ERROR/AndroidRuntime(24087): FATAL EXCEPTION: main
09-29> 09 11:06:51.624: ERROR/AndroidRuntime(24087): java.lang.RuntimeException: Unable to start receiver com.ashwin.demo.keyboard.KeyboardReceiver: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.ashwin.demo.keyboard/.Cs_key } without permission android.permission.BIND_INPUT_METHOD
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.os.Looper.loop(Looper.java:123)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at java.lang.reflect.Method.invokeNative(Native Method)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at java.lang.reflect.Method.invoke(Method.java:521)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at dalvik.system.NativeStart.main(Native Method)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087): Caused by: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.ashwin.demo.keyboard/.Cs_key } without permission android.permission.BIND_INPUT_METHOD
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.app.ContextImpl.startService(ContextImpl.java:832)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.content.ContextWrapper.startService(ContextWrapper.java:336)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.content.ContextWrapper.startService(ContextWrapper.java:336)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at com.ashwin.demo.keyboard.KeyboardReceiver.onReceive(KeyboardReceiver.java:18)
09-29 11:06:51.624: ERROR/AndroidRuntime(24087):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)

menifestfile

  <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="com.ashwin.demo.keyboard">
    <uses-permission android:name="android.permission.BIND_INPUT_METHOD"/>
    <application android:label="@string/app_name" >
        <service android:name="Cs_key" 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>
    <receiver android:name=".KeyboardReceiver"
    android:permission="android.permission.BIND_INPUT_METHOD">
    <intent-filter>
         <action  android:name="android.intent.action.TIME_SET"></action>
            </intent-filter>
    </receiver>
    </application>

     </manifest>

0 个答案:

没有答案