这是我的代码:
public class LocationIntentService extends IntentService{
JSONParser parser=new JSONParser();
public int idMax=0,success=-1;
private int idAccident,rating,idTypef;
private float logitude,latitude;
private String date;
public ArrayList<Accident> allAccidents=new ArrayList<>();
Accident accident;
public ArrayList<Accident> a=new ArrayList<>();
public LocationIntentService() {
super("LocationIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 9000, pendingIntent);
try {
Log.i(TAG, "service has now started");
new requestLocation().execute().get();
Log.i(TAG, ""+allAccidents.size());
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
private class requestLocation extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... params) {
double lat,lon;
String connect = Utility.lOCATION_SERVICE + "id=" + idMax;
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
JSONObject json = parser.makeHttpRequest(connect, "POST", params1);
if (json != null) {
try {
success = json.getInt("success");
idMax= json.getInt("idMax");
if (success == 1) {
JSONArray arr = json.getJSONArray("accidents");
for (int i = 0; i < arr.length(); i++) {
idAccident = arr.getJSONObject(i).getInt("idAccident");
lon = arr.getJSONObject(i).getDouble("longitude");
logitude= (float) lon;
lat = arr.getJSONObject(i).getDouble("latitude");
latitude= (float) lat;
rating= arr.getJSONObject(i).getInt("rating");
date= arr.getJSONObject(i).getString("date");
idTypef= arr.getJSONObject(i).getInt("idTypef");
accident=new Accident(idAccident,rating,idTypef,logitude,latitude,date);
allAccidents.add(accident);
}
}
}catch(JSONException e){
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute (String result){
}
}
我的日志显示大小正在改变arraylist所以它工作得很好但问题是我不知道如何从另一个类访问这个arraylist所以我将此方法添加到此类
public ArrayList<Accident> getAccidents(){
return this.allAccidents;
}
我从另一个班级尝试过这个:
ArrayList<Accident> all=new ArrayList<>();
LocationIntentService l=new LocationIntentService();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
all=l.getAccidents();
Toast.makeText(getApplicationContext(), " "+all.size(), Toast.LENGTH_LONG).show();
}
但问题是吐司显示为0 所以任何人都可以帮助我PLZ
答案 0 :(得分:0)
请更换此
@Override
protected void onHandleIntent(Intent intent) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 9000, pendingIntent);
try {
Log.i(TAG, "service has now started");
new requestLocation().execute().get();
Log.i(TAG, ""+allAccidents.size());
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
由此
private Handler handler;
@Override
protected void onHandleIntent(Intent intent) {
handler = new Handler();
Timer timer = new Timer();
TimerTask hourlyTask = new TimerTask() {
@Override
public void run () {
handler.post(new Runnable() {
@Override
public void run() {
try {
Log.i(TAG, "service has now started");
new requestLocation().execute().get();
Log.i(TAG, "size :"+allAccidents.size());
Log.i(TAG, "id : "+idMax);
}
catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(hourlyTask, 0l, 9000);
}