如何从Android服务获取数据?

时间:2012-12-13 21:18:59

标签: android google-maps android-service

我为我的应用程序创建了一个GPS服务,我想从GPS服务获取的数据用于我的另一个列表活动并显示它。问题是它无法显示在列表中。

启动GPS服务的启动代码:

public class splash extends Activity
{   
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.splash);


        new Handler().postDelayed(new Runnable() {
            public void run() {
                startService(new Intent("com.example.geotask.gpsservice"));
                    Intent mainIntent = new Intent(splash.this, MainActivity.class);
                    splash.this.startActivity(mainIntent);
                    splash.this.finish();
            }
        }, 1000);
     }

GPS服务代码:

public class GPSservice extends Service
{
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    public  ArrayList<String> position = new ArrayList<String>(); 
    public  ArrayList<Long> time=new ArrayList<Long>();
    public  ArrayList<Integer> lat= new ArrayList<Integer>(); 
    public  ArrayList<Integer> lon= new ArrayList<Integer>(); 
public void onCreate()
{
    super.onCreate();


}
public void onStart(Intent intent, int startID)
{LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}
private void additem(Location location){
     String latLongString = "Lat:" + location.getLatitude() + "\nLong:" + location.getLongitude();
     position.add(latLongString);
     long t=location.getTime();
     time.add(t);

}

private void addgeo(Location location){
    int x=(int)location.getLatitude()*1000000;
    int y=(int)location.getLongitude()*1000000;
    lat.add(x);
    lon.add(y);


}


LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            additem(location);
                    addgeo(location);
            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);
            sendBroadcast(intent);    }



        public void onProviderDisabled(String provider){


        }

        public void onProviderEnabled(String provider) {

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}
      };
}

列表活动代码:

public class list extends Activity{ 
    ArrayList details = new ArrayList();
    private BroadcastReceiver gpsreceiver;
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        IntentFilter intentFilter=new IntentFilter("com.example.geotask.gpsdata");
        intentFilter.addAction("com.example.geotask.gpsdata");
        gpsreceiver=new BroadcastReceiver()
        {
            @Override 
            public void onReceive(Context context, Intent intent)
            {
                List<String> ex=intent.getStringArrayListExtra("data");
                int i=0;
                for(i=0; i<ex.size(); i++)
                {
                    listdetails Detail = new listdetails();
                    Detail.setIcon(R.drawable.ic_launcher);
                    Detail.setPlace(ex.get(i));
                    Detail.setTime("2012.12.2");
                    details.add(Detail);
            }
            }
        };
        this.registerReceiver(gpsreceiver, intentFilter);


        setContentView(R.layout.list); 
        ListView listView; 
        listView = (ListView)findViewById(R.id.listView1);
        customlist Adapter = new customlist(details, this);
        listView.setAdapter(Adapter);


    }

1 个答案:

答案 0 :(得分:0)

您需要注册接收器

registerReceiver(gpsreceiver, intentFilter);

请记得取消注册onDestroy。