0

我有一个从 GCMBaseIntentService 扩展的 GCMIntentService 类,在这个类中,在 onMessage 函数中,我想调用一个 AsyncHttpResponseHandler。但是当我调用它时,

 /**
 * Method called on Receiving a new message from GCM server
 * */
@Override
protected void onMessage(final Context context, Intent intent) {

    if(aController == null)
        aController = (ProjectApplication) context.getApplicationContext();//for the json test use context.getapp

    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString(ProjectExtras.EXTRA_MESSAGE);

    aController.displayMessageOnScreen(context, message);

    JSONObject json;
    String mMessage="";
    int ID=1;
    int ACTION=0;

    if(message!=null){
        try {
             json= new JSONObject(message);
             mMessage= json.optString("Message");
             ID=json.optInt("u");
             ACTION=json.getInt("t");

        } catch (JSONException e) {
            Log.e("MESSAGE", "ERROR");
            e.printStackTrace();
        }
    }

    final int _ID =ID;
    final int _ACTION =ACTION;
    final String _mMessage =mMessage;

    getIntent(context, _ID, _ACTION,_mMessage);
}

这是我的函数,它有 SmartSyncManager.getInstance(context).getNews ,在这个函数中,我使用 AsyncHttpResponseHandler 从 Web 服务获取数据并更新我的本地数据库。

 private static void getIntent(final Context context, final int id, final int action , final String mMessage) {
    final Intent intent = new Intent(context, ProjectMainActivity.class);
    Log.d("IntentServiceId",""+ id);
    Log.d("IntentServiceAction",""+ action);

    switch (action) {
    case HABERLER:
        //intent = new Intent(context , NewsActivity.class);
        final Intent newsIntent = new Intent( context, NewsDetailActivity.class);

            Handler h = new Handler(Looper.getMainLooper());
            h.post(new Runnable() {
                @Override
                public void run() {
                    SmartSyncManager.getInstance(context).getNews(60*30*1000, Policy.LOCAL_IF_FAILS, new CompanyUpdateCallBack<CompanyDataObject>() {

                        @Override
                        public void onStart() {
                            Log.e("GCMIntService", "onStart getNews");
                        }

                        @Override
                        public void onSuccess(List<CompanyDataObject> objects) {
                            CompanyDataObject object =SmartSyncDataBaseHelper.getInstance(context).getNewsById(id);
                            if(object!=null){
                                newsIntent.putExtra(ProjectExtras.EXTRA_Company_MODULE, Constant.MODULE_NEWS);
                                newsIntent.putExtra(ProjectExtras.EXTRA_Company_DATA_OBJECT,object);
                                intent.setClass(context, NewsDetailActivity.class);

                            }
                            generateNotification(context, mMessage,id,intent);
                            Log.e("GCMIntService", "OnSuccess getNews");
                        }

                        @Override
                        public void onFail(List<IKOLDataObject> objects) {
                            Log.e("GCMIntService", "OnFail getNews");
                        }

                        @Override
                        public void onFinish() {
                            Log.e("GCMIntService", "onFinish getNews");
                        }});
                }
            });


        break;

它永远不会进入任何 AsyncHttpResponseHandler 函数,如成功或失败。甚至没有开始。然而它在活动课程中工作正常。也许是因为它在开关中?我尝试使用 runnable 让应用程序等待它,但效果不佳。

如何使 AsyncHttpResponseHandler(SmartSyncManager.getInstance(context).getSomething) 在 GCMIntentService 中工作?

4

1 回答 1

0

我只是将 getIntent 函数的所有内容移到 onMessage 中并且它工作正常。也改变了

 if(object!=null){
   newsIntent.putExtra(ProjectExtras.EXTRA_Company_MODULE, Constant.MODULE_NEWS);
   newsIntent.putExtra(ProjectExtras.EXTRA_Company_DATA_OBJECT,object);
   intent.setClass(context, NewsDetailActivity.class);
}   
generateNotification(context, mMessage,id,intent); 

进入

if(object!=null){
newsIntent.putExtra(ProjectExtras.EXTRA_Company_MODULE, Constant.MODULE_NEWS);
newsIntent.putExtra(ProjectExtras.EXTRA_Company_DATA_OBJECT,object);
intent.setClass(context, NewsDetailActivity.class);
generateNotification(context, mMessage,id,intent); 
}
于 2014-05-30T11:12:40.187 回答