我正在开发一个应用程序,其中我有一个共同的活动,即BaseActivity,我正在将它扩展到我的应用程序中的所有其他活动。我在 BaseActivity 中运行了几个 AsyncTask 以反映所有其他活动。我的查询是我需要从我的一项活动中将值传递给这个 BaseActivity,比如MainActivity
我尝试了类,接口概念,但不适合我。建议我,如何实现这一目标。如果需要我的任何参考代码,请评论它,我会发布它。
已编辑
我需要将此计数值传递给BaseActivity。我不能使用 Intent,因为我正在导航到另一个活动,比如MainActivity2
MainActivity 这是我的 AsyncTask 代码
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
@Override
protected Integer doInBackground(String... params) {
Integer result = 0;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
String response = streamToStringCount(httpResponse.getEntity().getContent());
parseResultCount(response);
result = 1;
} else {
result = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
// isRefreshing = false;
//layout.setRefreshing(false);
super.onPostExecute(result);
}
}
String streamToStringCount(InputStream stream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
if (null != stream) {
stream.close();
}
return result;
}
private void parseResultCount(String result) {
Detail_Item item;
try {
JSONObject posts = new JSONObject(result);
//rstatus1=posts.optString("count");
countValue=posts.optInt("count");
countValue1=countValue;
Log.i("countvalue", String.valueOf(countValue));
}
catch (JSONException e) {
e.printStackTrace();
}
}