1

在我的演示项目中,我正在使用GCM接收推送消息。我需要处理我以同步方式接收的推送消息。一旦我收到我的推送信息,我需要执行一些任务并将确认发送到我的服务器(确认由异步任务发送)。我的项目正在工作正常情况下,但如果我关闭数据连接,如果我给10推信息,然后我打开我的电话的数据连接,我的GCM被绞死,因为它接收消息作为一堆,之后,它不处理我推。 PLZ帮助解决这个问题Android:由于线程通知而等待Gcm被挂起并等待

MyGCMService.java

public class MyGCMService extends GCMBaseIntentService{ 
    ..... 

    public GCMIntentService() { 

     ... 
     myThreadClass =new MyThreadClass(); 

    } 

    @Override 
    protected void onRegistered(Context context, String registrationId) { 

     .... 
    } 

    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     .... 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 

     try{ 
     // System.out.println("*********** 4-3- "+String.valueOf(myThreadClass.getState()).equals("NEW")); 
      if(String.valueOf(myThreadClass.getState()).equals("NEW")) 
     myThreadClass.start(); 
     }catch(Exception e){ 

     } 
     synchronized (myThreadClass) { 
      ... 
      myThreadClass.wait(); 
     } 

    } 


    @Override 
    protected void onDeletedMessages(Context context, int total) { 
     ... 
    } 

    @Override 
    public void onError(Context context, String errorId) { 
     .... 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     .... 
    } 


    public void OnDestroy() { 

     super.onDestroy(); 

    }  
} 

MyTreadClass.java

public class MyThreadClass extends Thread { 


    MyThreadClass myThreadClass; 
    String LOG_TAG = MyThreadClass.class.getSimpleName(); 

    public void run() { 
     synchronized (this) { 

      Looper.prepare(); 

      performAction(); 

      notify(); 

     } 

    } 

    public MyThreadClass() { 
     myThreadClass=this; 
    } 



    public void performMDMAction() { 

     //Doing Some task and Sending Ack. through Async task 
    } 



} 

一旦这个线程挂起我的GCMBaseIntentService,在覆盖的onMessage()函数不叫..

在前提前感谢

回答

0

你真的不应该使用低等级的同步方法,如等待和通知。正确使用它们非常棘手。 如果你想在Android应用程序中执行异步任务,或许AsyncTask将适合你的需求。如果不是,请考虑使用java.util.concurrent包。