Question

I'm trying to write a simple app that updates the MainActivity with the Lat/Lng values returned by the service. But it always returns the null value. I have added permissions and added TheService.java as service in AndroidManifest.xml file...Kindly tell me where I have gone wrong.

MainActivity.java

public class MainActivity extends Activity {

TextView tv1, tv2;
IntentFilter filter;
receive rec;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 =(TextView)findViewById(R.id.textView1);
    tv2 = (TextView)findViewById(R.id.textView2);
    filter = new IntentFilter("Updated");
    rec = new receive();
    Intent gps_int = new Intent(this,TheService.class);
    startService(gps_int);      

}

@Override
public void onPause()
{
    super.onPause();
    unregisterReceiver(rec);
}

@Override
public void onResume()
{
    super.onResume();
    rec = new receive();
    registerReceiver(rec, filter);      
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class receive extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        tv1.setText(intent.getExtras().getString("lat"));
        tv2.setText(intent.getExtras().getString("lon"));
        Toast.makeText(getApplicationContext(), "Toast Executed", Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), "BR Latitude "+intent.getExtras().getString("lat"),Toast.LENGTH_SHORT).show();
    }

}}

TheService.java

public class TheService extends Service implements LocationListener {
LocationManager lm;
Location loc;
double lat = 0;
double lon = 0;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    lm=(LocationManager)getSystemService(LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
    loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    onLocationChanged(loc);
     return START_STICKY;
}
@Override
public void onCreate()
{


}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    updateui(location);
}

private void updateui(Location location) {
    // TODO Auto-generated method stub
    lat = location.getLatitude();
    lon = location.getLongitude();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Intent gps_intent = new Intent("Updated");
    gps_intent.putExtra("lat", lat);
    gps_intent.putExtra("lon", lon);
    sendBroadcast(gps_intent);
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}}
Was it helpful?

Solution

  1. You should use the Google Play Services for the location, that's easier to handle.
  2. What for do you even need a Service? Having a location listener in your Activity is totally fine.
  3. If you want to stay with the Service, than bind the Activity to it, instead of using a Broadcast.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top