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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...