android – 使用服务绑定从服务更新活动TextView

前端之家收集整理的这篇文章主要介绍了android – 使用服务绑定从服务更新活动TextView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有阅读GPS纬度和经度的服务.我想从服务的locationlistener的onLocationChanged更新活动.

我怎样才能实现它?我一直在阅读Service Bound但看起来它只是用于调用服务中的方法的活动而不是服务调用Activity中的textView.绑定服务是不是可以实现呢?

解决方法

您应该使用服务中的 LocalBroadcastManager类将Intent发送回活动.

例如,包含单个TextView的Activity可以设置如下的BroadcastReceiver:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = (TextView) findViewById(R.id.main_activity_text_view);

        LocalBroadcastManager.getInstance(this).registerReceiver(
                new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context,Intent intent) {
                        double latitude = intent.getDoubleExtra(LocationBroadcastService.EXTRA_LATITUDE,0);
                        double longitude = intent.getDoubleExtra(LocationBroadcastService.EXTRA_LONGITUDE,0);
                        textView.setText("Lat: " + latitude + ",Lng: " + longitude);
                    }
                },new IntentFilter(LocationBroadcastService.ACTION_LOCATION_BROADCAST)
        );
    }

    @Override
    protected void onResume() {
        super.onResume();
        startService(new Intent(this,LocationBroadcastService.class));
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopService(new Intent(this,LocationBroadcastService.class));
    }
}

基本服务可以广播所有位置更改,如下所示:

public class LocationBroadcastService extends Service {

    public static final String
            ACTION_LOCATION_BROADCAST = LocationBroadcastService.class.getName() + "LocationBroadcast",EXTRA_LATITUDE = "extra_latitude",EXTRA_LONGITUDE = "extra_longitude";

    private static final int
            MIN_TIME = 2000,MIN_DISTANCE = 1;

    @Override
    public void onCreate() {
        super.onCreate();
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        sendBroadcastMessage(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME,MIN_DISTANCE,new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        sendBroadcastMessage(location);
                    }

                    @Override
                    public void onStatusChanged(String provider,int status,Bundle extras) {

                    }

                    @Override
                    public void onProviderEnabled(String provider) {

                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                }
        );
    }

    private void sendBroadcastMessage(Location location) {
        if (location != null) {
            Intent intent = new Intent(ACTION_LOCATION_BROADCAST);
            intent.putExtra(EXTRA_LATITUDE,location.getLatitude());
            intent.putExtra(EXTRA_LONGITUDE,location.getLongitude());
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

猜你在找的Android相关文章