Android GCM sometimes app crashes because com.google.android.gms.gcm.GcmListenerService is rejected from java.util.concurrent.ThreadPoolExecutor

杀马特。学长 韩版系。学妹 提交于 2019-12-11 14:02:21

问题


since I upgraded GCM to 8.4.0 my app sometimes crashes and the log shows the following exception:

Fatal Exception: java.lang.RuntimeException: Unable to start service com.myapp.app.Service_GCMListenerService@ea3f6c7 with Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000010 pkg=com.myapp.app cmp=com.myapp.app/.Service_GCMListenerService (has extras) }: java.util.concurrent.RejectedExecutionException: Task com.google.android.gms.gcm.GcmListenerService$1@7b28d65 rejected from java.util.concurrent.ThreadPoolExecutor@f392a3a[Running, pool size = 9, active threads = 9, queued tasks = 128, completed tasks = 0]
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
       at android.app.ActivityThread.-wrap17(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by java.util.concurrent.RejectedExecutionException: Task com.google.android.gms.gcm.GcmListenerService$1@7b28d65 rejected from java.util.concurrent.ThreadPoolExecutor@f392a3a[Running, pool size = 9, active threads = 9, queued tasks = 128, completed tasks = 0]
       at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2014)
       at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:794)
       at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1340)
       at com.google.android.gms.gcm.GcmListenerService.zzn(Unknown Source)
       at com.google.android.gms.gcm.GcmListenerService.onStartCommand(Unknown Source)
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
       at android.app.ActivityThread.-wrap17(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

This crash does not explain where is the error in my code. What should I do?

This is my implementation of GCM:

Android Manifest:

<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" />
            <category android:name="com.myapp.app" />
        </intent-filter>
    </receiver>

    <service
        android:name=".Service_GCMListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <service
        android:name=".Service_IDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>

Service_GCMListenerService

public class Service_GCMListenerService extends GcmListenerService {



    @Override
    public void onDeletedMessages() {
        super.onDeletedMessages();

        PushManager.checkForNewPush();
    }

    @Override
    public void onMessageReceived(String from, Bundle data) {
        super.onMessageReceived(from, data);

        PushManager.processPush(this,false,data.getString("message"));
    }


}

Service_IDListenerService

public class Service_IDListenerService extends InstanceIDListenerService {

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. This call is initiated by the
 * InstanceID provider.
 */
@Override
public void onTokenRefresh() {
    GCMUtils.registerNewInstanceID();
}

回答1:


AsyncTask's THREAD_POOL_EXECUTOR has a hard-coded limit of 128 tasks queued. Your problem is that you are executing a lot of tasks on that executor.

If you are not directly calling executeOnExecutor on any of your AsyncTasks, the most likely culprit are third-party methods that return a ListenableFuture, especially those performing network operations. You'll probably want a way to execute those serially. Moving all of those operations into AsyncTasks executing on AsyncTask.SERIAL_EXECUTOR would be one solution.



来源:https://stackoverflow.com/questions/37273382/android-gcm-sometimes-app-crashes-because-com-google-android-gms-gcm-gcmlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!