Google 播放服务错误:读取失败:EPERM(不允许操作)

Error in Google Play Services: read failed: EPERM (Operation not permitted)

这是我收到的 LogCat 错误:

android java.io.IOException: read failed: EPERM (Operation not permitted)
    at libcore.io.IoBridge.read(IoBridge.java:435)

错误出现在以下代码中,我在其中尝试使用 AsyncTask 注册设备:

private void registerInBackground(){
    mRegisterIdTask = new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regId = gcm.register(SENDER_ID);
                msg = "Device registered, registration ID=" + regId;
                /* TODO : Send regId to backend to store in database so backend can send...
                   TODO : ...push notification request to GooglePlayMessaging
                 */
                sendRegistrationIdToBackend();
                storeRegistrationId(context, regId);
            } catch (IOException e) {
                msg = "Error : " + e.getMessage();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i("Google Play", "Status : Finished ");
            mRegisterIdTask = null;
        }
    }.execute(null, null, null);
}

好吧,现在我明白了,我没有改变任何东西。

05-15 17:33:35.253  16020-16271/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.panicsystems.kyuubi.legalpanicpartner, PID: 16020
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.SecurityException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gms (has extras) } without permission com.google.android.c2dm.permission.RECEIVE
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1603)
        at android.app.ContextImpl.startService(ContextImpl.java:1580)
        at android.content.ContextWrapper.startService(ContextWrapper.java:494)
        at com.google.android.gms.gcm.GoogleCloudMessaging.zzs(Unknown Source)
        at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
        at com.kyuubi.MainActivity.doInBackground(MainActivity.java:171)
        at com.kyuubi.MainActivity.doInBackground(MainActivity.java:161)
        at android.os.AsyncTask.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

如果您真的阅读了日志,就会很清楚问题出在哪里。

您缺少使用 GCM 所需的权限。

Caused by: java.lang.SecurityException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gms (has extras) } without permission com.google.android.c2dm.permission.RECEIVE

在 AndroidManifest.xml 内添加:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

并确保您已注册 GCM 接收器

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <!-- support pre-4.4 KitKat devices -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.company.app" />
            </intent-filter>
        </receiver>

和您的服务:

 <service
       android:name="com.company.app.service.GcmRegistrationIntentService"
            android:exported="false" >
 </service>