私は共通のアクティビティ、つまりBaseActivityを持つアプリケーションを開発しています。アプリケーション内の他のすべてのアクティビティに拡張しています。BaseActivity でいくつかの AsyncTask を実行して、他のすべてのアクティビティに反映させます。私のクエリは、アクティビティの 1 つからこの BaseActivity に値を渡す必要があるということです。
クラス、インターフェイスの概念を試しましたが、うまくいきませんでした。これを達成する方法を教えてください。私の参照コードが必要な場合は、コメントしてください。投稿します。
編集済み
このcountvalueをBaseActivityに渡す必要があります。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();
}
}