我有一个从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函数中,比如on success或on fail。甚至在开始的时候都没有。然而,它在活动课上工作得很好。也许是因为它在开关里?我尝试使用runnable让应用程序等待它,但它不工作好。
我如何使AsyncHttpResponseHandler(SmartSyncManager.getInstance(context).getSomething)在GCMIntentService工作?
我只是把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);
}