我为我的应用程序创建了一个 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);
}