gpt4 book ai didi

android - 如何保持轮询绑定(bind)服务?

转载 作者:行者123 更新时间:2023-11-29 15:43:37 26 4
gpt4 key购买 nike

我在互联网上查找,但找不到涵盖我的场景的示例。我想做的是:

1) To start and bind to a service as soon as my activity starts (done)

2) The service then binds itself to another service looking for a user input from a connected device, and saves a string a string to a variable (done)

3) I would like to send back this string to the activity, so I can check what it is and based on it to make a network call.

现在第 3 个)是我的挑战。我设法用一个运行一秒钟的计时器来做到这一点,然后检查服务中写入的值,但不知何故这似乎不是正确的方法,我认为可能有更成熟的解决方案。但是,我似乎无法弄清楚。

我从文档中获取了代码,只添加了计时器。在这个例子中,只有一个服务只生成一个随机数(这通常会被我的第二个服务取代)。这是服务的代码:

public class LocalService extends Service {

private final IBinder mBinder = new LocalBinder();

private final Random mGenerator = new Random();

public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}

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

public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}

这是我 Activity 中的代码:

public class MainActivity extends AppCompatActivity {

LocalService mService;
boolean mBound = false;
Timer timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

timer = new Timer();
}

@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

timer.schedule(new MyTimerTask(new Handler(), this), 1000, 1000); // run on every second
}

@Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(mConnection);
mBound = false;
}

timer.cancel();
timer.purge();
}

private class MyTimerTask extends TimerTask {
Handler handler;
MainActivity ref;

public MyTimerTask(Handler handler, MainActivity ref) {
super();
this.handler = handler;
this.ref = ref;
}

@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
if (mBound) {
int num = ref.mService.getRandomNumber();
// just as an example, raise a toast to see if it works
// but otherwise the value will be handled
Toast.makeText(ref, "number: " + num, Toast.LENGTH_SHORT).show();
}
}
});
}
}

private ServiceConnection mConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}

@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
}

我的问题是:这是一种好方法(有效)还是不好,有什么替代方法?

最佳答案

您可以使用 LocalBroadcastManager 将广播从您的 Service 发送到您的 Activity。例如,在您的 Service 中声明:

public static final String BROADCAST_INTENT = "broadcast_intent";
public static final String BROADCAST_VALUE = "broadcast_value";

private LocalBroadcastManager broadcastManager;

public void onCreate() {
super.onCreate();

broadcastManager = LocalBroadcastManager.getInstance(this);
}

现在,无论何时你想发送一个 String 到你的 Activity,你都可以这样做:

private void sendBroadcast(String value) {

Intent intent = new Intent(BROADCAST_INTENT);
intent.putExtra(BROADCAST_VALUE, value);
broadcastManager.sendBroadcast(intent);
}

在您的Activity 中声明一个BroadcastReceiver:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {

handleIntent(intent);
}
};

当您绑定(bind)到您的服务时注册接收器:

IntentFilter broadcastIntentFilter = new IntentFilter();
broadcastIntentFilter.addAction(StreamService.BROADCAST_INTENT);
LocalBroadcastManager.getInstance(context).registerReceiver((broadcastReceiver), broadcastIntentFilter);

并在您解除服务绑定(bind)的地方取消注册:

LocalBroadcastManager.getInstance(context).unregisterReceiver(broadcastReceiver);

现在,当您的服务发送广播时,您可以在 Activity 中处理它:

private void handleIntent(Intent intent) {

if (intent.getAction().equals(StreamService.BROADCAST_INTENT)) {
String value = intent.getStringExtra(StreamService.BROADCAST_VALUE, "default");
}
}

关于android - 如何保持轮询绑定(bind)服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36499913/

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