我正在開發一個應用程序,我有一個共同的活動,即BaseActivity,我將它擴展到我的應用程序中的所有其他活動。 我在BaseActivity中運行一些AsyncTask以反映所有其他活動。我的查詢是我需要從我的活動之一將值傳遞給此BaseActivity,說MainActivity如何將值從一個活動發送到另一個在android中的擴展活動?
我試過類,接口的概念,但沒有爲我工作。建議我,如何實現這一點。如果我的任何參考代碼需要評論,我會發布它。
EDITED
我需要這個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();
}
}
在基本活動中需要哪種類型的值? –
你可以顯示你的代碼你試過了嗎? – Masum
我從** MainActivity **獲取幾個整數值,並需要將這些整數值傳遞給** BaseActivity ** –