I am trying to populate an ArrayList called items in a service, however everything I call service from my activity only the first element is populated into the array. I am not sure what is the best way to do this. I heard about using bind service methods but from what I read this is not adviced.
• 如何在服务中断之前将体积储存在顽固的阵列中
Thanks in advance,
PROBLEM CODE
public void print_result(String orig) {
Log.d(TAG, "HELLO WORLD:" + orig);
int i = 0;
items = new ArrayList<String>();
if (items != null) {
if (items.contains(orig)) {
Log.d(TAG, "Element already exists exiting");
} else {
items.add(orig);
Log.d(TAG, "Adding Element" + items);
}
} else {
Log.d(TAG, "IS NULL");
}
}
Graham
ACTIVITY
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop, buttonAdd;
EditText Input;
String x = "";// test pass to my service
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
Input = (EditText) findViewById(R.id.INPUT);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
Intent dataIntent = new Intent(ServicesDemo.this, MyService.class);
x = Input.getText().toString();
dataIntent.putExtra("originator", x);
startService(dataIntent);
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
// implicit starting
stopService(new Intent(this, MyService.class));
break;
case R.id.buttonAdd:
Log.d(TAG, "ADDING VALUES");
break;
}
}
public static String getTag() {
return TAG;
}
}
SERVICE
package com.example;
import java.util.ArrayList;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
ArrayList<String> items = null;
public String ORIG = "";
private static final String TAG = "MyService";
public Bundle data = new Bundle();
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static String getTag() {
return TAG;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart");
data = intent.getExtras();
ORIG = data.getString("originator");
Log.d(TAG, "onCreate");
Thread initBkgdThread = new Thread(new Runnable() {
public void run() {
print_result(ORIG);
}
});
initBkgdThread.start();
}
public void print_result(String orig) {
Log.d(TAG, "HELLO WORLD:" + orig);
int i = 0;
items = new ArrayList<String>();
if (items != null) {
if (items.contains(orig)) {
Log.d(TAG, "Element already exists exiting");
} else {
items.add(orig);
Log.d(TAG, "Adding Element" + items);
}
} else {
Log.d(TAG, "IS NULL");
}
}
}