I Create a GPSservice for my app and I would like to get the data obtained from the GPSservice for my another list activity and show it. The problem is that it cannot be shown in the list.
the splash code to start the GPSservice:
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);
}
The GPSservice code:
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) {}
};
}
The list activity code:
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);
}
You need to register your receiver
Remember to unregister onDestroy.