1

我创建了一个 gpsservice,一个带有两个按钮的主要活动和一个列表活动。我想在获取gps数据时获取gpsservie数据,所以我在主activity中创建了一个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);

            }

            }
4

2 回答 2

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");
        }

这可确保您现在正在修改全局ArrayList。此外,您可能需要将全局 ArrayLists 声明为final,因为 Java 可能会将您new BroadCastReceiver视为匿名内部类。

于 2012-12-14T15:58:58.253 回答
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();
    }

    }
于 2015-03-28T11:34:59.867 回答