从处理程序传递布尔值到函数

时间:2013-09-04 22:40:53

标签: android return handler

我正在尝试开发一个函数,为我的Android应用程序提供测试,以查看手机上的数据是否与服务器上的数据相匹配。

除了我希望消息从服务器返回到处理程序之外,我让函数的每个部分工作正常然后我希望处理程序返回false或true并将值传递给返回布尔值的函数。

非常感谢正确方向上的一点。

这是目前为止的Android代码。

public boolean isTripUpladedToServer(int tripId)
{
    if(isServiceRunning()&&tripId==currentTripId){return false;}
    SQLiteDatabase db;
    db=this.openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);
    String Qu="SELECT COUNT(tripid) from TRIP_DATA WHERE TRIPID="+tripId+";";
    Cursor c= db.rawQuery(Qu, null);
    int count=0;
    if(c!=null &&c.moveToFirst())
    {
        count=c.getInt(0);
    }
    JSONArray parcel =new JSONArray();
    JSONObject header =new JSONObject();
    JSONObject message =new JSONObject();
    try {
        header.put("tablename", "isTripUploaded");
        header.put("userid", userid);
        parcel.put(header);
        message.put("count", count);
        message.put("tripid", tripId);
        parcel.put(message);
        Log.i(tag, parcel.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Handler inner=new Handler()
    {   
        @Override
        public void handleMessage(Message msg)
        {
            try {
                JSONObject ret=new JSONObject(msg.obj.toString());
                Log.i(tag,ret.toString());


          // I want the function to return the boolean value that the server has sent to phone.


} catch (JSONException e) {

                e.printStackTrace();
            }

        }
    };

    new uploadero(inner).execute(parcel);

  //the below return value is here to prevent the error, ideally I want to remove it
    return false;
}

如果我以错误的方式接近这一点,请提前谢谢Mark

1 个答案:

答案 0 :(得分:1)

对布尔值使用类变量,并在处理程序中使用getter来获取它。

相关问题