使用服务绑定从服务更新 Activity TextView [英] Updating Activity TextView from service using Service binding

查看:22
本文介绍了使用服务绑定从服务更新 Activity TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个读取 GPS 纬度和经度的服务.我想从服务的 locationlisteneronLocationChanged 更新活动.

I have a service for reading GPS latitude and longitude. I would like to update the activity from the service's locationlistener's onLocationChanged.

我怎样才能实现它?我一直在阅读 Service Bound 但它看起来像仅用于活动调用服务中的方法,而不是服务调用活动中的 textView.难道绑定服务就不能实现了吗?

How can I achieve it? I have been reading on Service Bound but it looks like it is only for activity to invoke the methods in service but not service invoke textView in Activity. Is binding service is not possible to achieve it?

推荐答案

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

You should use the LocalBroadcastManager class from within your Service to send an Intent back to the Activity.

例如,包含单个 TextViewActivity 可以像这样设置 BroadcastReceiver:

For Example, An Activity containing a single TextView can set up a BroadcastReceiver like such:

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));
    }
}

一个基本的Service可以广播所有的位置变化如下:

And a basic Service can broadcast all location changes as follows:

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;
    }
}

这篇关于使用服务绑定从服务更新 Activity TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆