我在活动中使用 asynctask 编写了一些代码。但是希望在单独的 java 文件中编写的服务类中重用相同的代码。我试图在一个单独的 java 文件中编写代码,以便我可以在两者上使用它。但它并没有为 asynctask 锻炼。有没有办法这样做。请提供一些它的教程,如果有的话。谢谢。代码是这样的。
MainActivity.java
//import java.util.Calendar;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlarmManager;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class AndroidJSONParsingActivity extends ListActivity {
protected static final String TAG_PRODUCTS = "products";
protected static final String TAG_CID = "cid";
public static final String TAG_NAME = "name";
JSONArray products = null;
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
public static String url = "http://ensignweb.com/sandbox/app/comment11.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(this, UpdateService.class));
new Messages().execute();
Intent intent = new Intent(this,UpdateService.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, pIntent);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
new Messages().execute();
}
//Belongs to update service
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// stopService(new Intent(this, UpdateService.class));
}
class Messages extends AsyncTask<Void, Void, Void> {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
ProgressDialog dialog = null;
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
dialog.setTitle("Progressing");
dialog.setMessage("be patient");
JSONParser JSP = new JSONParser();
JSONObject json = JSP.getJSONFromUrl(url);
try{
products = json.getJSONArray(TAG_PRODUCTS);
for(int i = products.length()-1; i >=0; i--){
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String cid = c.getString(TAG_CID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CID, cid);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
contactList.add(map);
Log.d("value", contactList.toString());
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// dialog.dismiss();
ListAdapter adapter = new SimpleAdapter(AndroidJSONParsingActivity.this, contactList,
R.layout.list_item,
new String[] { TAG_NAME,}, new int[] {
R.id.name});
AndroidJSONParsingActivity.this.setListAdapter(adapter);
}
}
}