gpt4 book ai didi

android - 使用服务绑定(bind)从服务更新 Activity TextView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:52 26 4
gpt4 key购买 nike

我有一个读取 GPS 纬度和经度的服务。我想从服务的 locationlisteneronLocationChanged 更新 Activity 。

我怎样才能实现它?我一直在阅读 Service Bound但看起来它只是为了 Activity 调用服务中的方法而不是服务调用 Activity 中的textView。难道绑定(bind)服务不是可以实现的吗?

最佳答案

你应该使用 LocalBroadcastManager从您的服务中发送一个 Intent 返回给 Activity。

例如,包含单个 TextViewActivity 可以像这样设置一个 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 - 使用服务绑定(bind)从服务更新 Activity TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23968240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com