使用从服务返回的纬度/经度值更新活动

时间:2013-07-16 16:39:59

标签: android service gps android-activity locationlistener

我正在尝试编写一个简单的应用程序,使用服务返回的Lat / Lng值更新MainActivity。但它总是返回null值。我添加了权限并在AndroidManifest.xml文件中添加了TheService.java作为服务...请告诉我哪里出错了。

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

}}

1 个答案:

答案 0 :(得分:1)

  1. 您应该使用Google Play Services作为位置,这样更容易处理。
  2. 您甚至需要什么服务?在您的Activity中拥有位置监听器是完全没问题的。
  3. 如果您希望继续使用服务,而不是使用bind活动,而不是使用广播。