我試圖創建一個全局動作文件,我可以在應用程序中的任何地方全局調用某些函數。到目前爲止,我已經得到它與函數等工作,但現在我想要做的是把一個標準的AsyncTask到我的全局行動,並從我的活動傳遞它的函數/空白,所以我想要傳遞一個函數來運行背景和一次運行完成後的功能。有誰知道這是怎麼回事?並且還建議我在後臺線程上運行多個函數我是否創建一個AsyncTask並以多個函數提供或者是否創建多個AsyncTasks?將函數傳遞到全局AsyncTask Android
繼承人我到目前爲止設法做到的全球範例以及我目前如何實施我的Asynctask。只是爲了重申我試圖將asynctask移動到全局行爲並儘可能重複使用
在我的全局動作im創建一個函數,制定一個URL併發送post變量到該url然後反饋一個JSON響應。然後在我的活動我已經創建了以下兩種功能調用請求,然後記錄響應
我的全球行動
public final static JSONObject startAPICallRequest(Context activityContext, String requestReference, String callLocation, Map<String, String> postVarsMap, Map<String, String> getVarsMap){
long unixTimeStamp = System.currentTimeMillis()/1000L;
StringBuilder extraGetVarsString = new StringBuilder();
if(getVarsMap != null){
Map<String, String> map = (Map)getVarsMap;
for (Entry<String, String> entry : map.entrySet()) {
extraGetVarsString.append("&" + entry.getKey() + "=" + entry.getValue());
extraGetVarsString.toString();
}
}
String appVersion = null;
try {
appVersion = activityContext.getPackageManager().getPackageInfo(activityContext.getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
appVersion = activityContext.getResources().getString(R.string.appVersion);
}
String getVarsString = "?timestamp=" + unixTimeStamp + "&app_version=" + appVersion + extraGetVarsString;
String apiLocation = activityContext.getResources().getString(R.string.apiLocation);
String fullAPIURL = apiLocation + callLocation + getVarsString;
Log.v("globals", "fullAPIURL=" + fullAPIURL);
String api_key = activityContext.getResources().getString(R.string.apiKey);
String api_user = activityContext.getResources().getString(R.string.apiUser);
String request_token = returnSHAFromString(api_key, fullAPIURL);
String device_id = returnStringFromPreference(activityContext,"device_id");
String user_token = returnStringFromPreference(activityContext,"user_token");
List<NameValuePair> postVarsList = new ArrayList<NameValuePair>();
postVarsList.add(new BasicNameValuePair("request_token", request_token));
postVarsList.add(new BasicNameValuePair("api_user", api_user));
postVarsList.add(new BasicNameValuePair("device_id", device_id));
postVarsList.add(new BasicNameValuePair("user_token", user_token));
if(postVarsMap != null){
Map<String, String> map = (Map)postVarsMap;
for (Entry<String, String> entry : map.entrySet()) {
postVarsList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
JSONObject responseJSON = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(fullAPIURL);
post.setEntity (new UrlEncodedFormEntity(postVarsList));
HttpResponse response = client.execute(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String jsonResponse = reader.readLine();
Log.v("globals", "postList =" + postVarsList);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseJSON;
}
我的活動
public void apiCall(){
responseJSON = GlobalActions.startAPICallRequest(this, "login", "my-network/", null, null);
}
public class PostTask extends AsyncTask<Void, String, Boolean> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Void... params) {
apiCall();
boolean result = false;
publishProgress("progress");
return result;
}
protected void onProgressUpdate(String... progress) {
StringBuilder str = new StringBuilder();
for (int i = 1; i < progress.length; i++) {
str.append(progress[i] + " ");
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
checkLoginData();
}
}
public void checkLoginData(){
Log.v("IntroLoader", "responseJSON = " + responseJSON);
Intent register = new Intent(getApplicationContext(), LoginForm.class);
startActivity(register);
}
有趣的是,從未使用intentservice之前,你能告訴我如何在我的代碼中實現它嗎? – 2013-02-15 09:18:12