我创建了一个gpsservice,一个带有两个按钮和一个列表活动的主活动。我想在获取gps数据时获取gpsservie数据,因此我在主活动中创建了一个gps接收器来获取数据。当我单击主活动中的按钮时,它将转到列表活动。我想在单击按钮时使用intent()将获取的数据传输到列表活动。问题是当我点击按钮时,列表视图中没有显示任何内容。
GPS服务的关键代码:
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
additem(location);
addgeo(location);
//String latLongString = "Lat:" + location.getLatitude() + "\nLong:" + location.getLongitude();
Intent intent= new Intent("com.example.geotask.gpsdata");
intent.putIntegerArrayListExtra("lat", (ArrayList<Integer>) lat);
intent.putIntegerArrayListExtra("lon", (ArrayList<Integer>) lon);
intent.putStringArrayListExtra("data", (ArrayList<String>) position);
//intent.putExtra("data", latLongString);
sendBroadcast(intent); }
主要活动代码:
public class MainActivity extends Activity {
public ArrayList<String> position1 = new ArrayList<String>();
public ArrayList<Long> time=new ArrayList<Long>();
public ArrayList<Integer> lat1= new ArrayList<Integer>();
public ArrayList<Integer> lon1= new ArrayList<Integer>();
private BroadcastReceiver gpsreceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter=new IntentFilter("com.example.geotask.gpsdata");
gpsreceiver=new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
ArrayList<String> position1=intent.getStringArrayListExtra("data");
ArrayList<Integer> lat1=intent.getIntegerArrayListExtra("lat");
ArrayList<Integer> lon1=intent.getIntegerArrayListExtra("lon");
}
};
this.registerReceiver(gpsreceiver, intentFilter);
final Button listbutton=(Button) findViewById(R.id.button2);
listbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent =new Intent();
intent.setClass(MainActivity.this, list.class);
intent.putStringArrayListExtra("data", (ArrayList<String>) position1);
startActivity(intent);
}
});
}
列表活动:
public class list extends Activity{
static ArrayList<listdetails> details = new ArrayList<listdetails>();
// private BroadcastReceiver gpsreceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView listView;
listView = (ListView)findViewById(R.id.listView1);
Intent intent=getIntent();
List<String> ex=intent.getStringArrayListExtra("data");
customlist Adapter = new customlist(details, this);
listView.setAdapter(Adapter);
listdetails Detail = new listdetails();
for(int i=0;i<ex.size();i++)
{
Detail.setIcon(R.drawable.ic_launcher);
Detail.setPlace(ex.get(i));
Detail.setTime("2012.12.2");
details.add(Detail);
}
}
答案 0 :(得分:0)
您正在onReceive()
复制变量。这意味着ArrayList
变量在onReceive()
完成后会被销毁。您的onReceive()
应如下所示:
@Override
public void onReceive(Context context, Intent intent)
{
position1=intent.getStringArrayListExtra("data");
lat1=intent.getIntegerArrayListExtra("lat");
lon1=intent.getIntegerArrayListExtra("lon");
}
这可确保您现在正在修改全局 ArrayLists。此外,您可能需要将全局ArrayLists声明为 final ,因为Java可能会将您的new BroadCastReceiver
视为匿名内部类。
答案 1 :(得分:0)
您可以启动服务并将其与活动绑定并从服务中获取数据
活动代码
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by dheeraj on 24/3/15.
*/
public class DownloadActivity extends Activity {
private Handler handler;
private ProgressBar progressBar;
private TextView textView;
private int k = -100;
int status;
private boolean run = true;
MyServices myServices;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
myServices = ((MyServices.MyLocalBinder) binder).getService();
Log.i("", "");
Thread thread =new Thread(new Runnable() {
@Override
public void run() {
while (run) {
try {
k = myServices.getData();
} catch (Exception e) {
Log.e("", "", e);
return;
}
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(k));
}
});
} catch (Exception e) {
Log.e("", "", e);
}
try {
Thread.sleep(2000);
} catch (Exception e) {
Log.e("", "", e);
return;
}
}
Log.e("","");
}
});
thread.setName("Activity Thread");
thread.start();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("", "");
run = false;
}
};
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
textView = (TextView) findViewById(R.id.title);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
Intent intent = new Intent(getApplicationContext(), MyServices.class);
startService(intent);
bindService(intent, serviceConnection, 0);
}
@Override
public void onDestroy() {
unbindService(serviceConnection);
super.onDestroy();
}
}
服务代码
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Created by dheeraj on 24/3/15.
*/
public class MyServices extends Service {
private int data = 0;
private MyRunnable myRunnable;
public int getData(){
return data;
}
private class MyRunnable implements Runnable{
private boolean run = true;
public void setRun(){
run = false;
}
@Override
public void run() {
while(run){
if(data > 100){
stopSelf();
return;
}
data = data + 5;
try {
Thread.sleep(2000);
}catch (Exception e){
}
}
}
}
private final IBinder myBinder = new MyLocalBinder();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return myBinder;
}
public class MyLocalBinder extends Binder {
public MyServices getService() {
return MyServices.this;
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
myRunnable = new MyRunnable();
Thread thread =new Thread(myRunnable);
thread.setName("Service Thread");
thread.start();
return START_STICKY;
}
public void onDestroy() {
if(myRunnable != null){
myRunnable.setRun();
}
super.onDestroy();
}
}