如何保持轮询绑定服务?
How to keep polling a bound service?
我在互联网上查找,但找不到涵盖我的场景的示例。我想做的是:
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;
}
};
}
我的问题是:这是一个好方法(有效)还是不好,有什么替代方法?
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.
使用 LocalBroadcastManager
、greenrobot 的 EventBus、Square 的 Otto 或其他一些进程内事件总线实现。更改数据时引发事件。让 activity 在公共汽车上注册以了解该事件。让 activity 在发生更改时使用更改后的数据。
is this a good approach
没有
您可以使用 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);
}
现在,无论何时您想向您的 Activity
发送 String
,您都可以这样做:
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);
}
};
绑定到您的 Service
时注册接收器:
IntentFilter broadcastIntentFilter = new IntentFilter();
broadcastIntentFilter.addAction(StreamService.BROADCAST_INTENT);
LocalBroadcastManager.getInstance(context).registerReceiver((broadcastReceiver), broadcastIntentFilter);
并在解除绑定的地方取消注册 Service
:
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");
}
}
我在互联网上查找,但找不到涵盖我的场景的示例。我想做的是:
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;
}
};
}
我的问题是:这是一个好方法(有效)还是不好,有什么替代方法?
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.
使用 LocalBroadcastManager
、greenrobot 的 EventBus、Square 的 Otto 或其他一些进程内事件总线实现。更改数据时引发事件。让 activity 在公共汽车上注册以了解该事件。让 activity 在发生更改时使用更改后的数据。
is this a good approach
没有
您可以使用 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);
}
现在,无论何时您想向您的 Activity
发送 String
,您都可以这样做:
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);
}
};
绑定到您的 Service
时注册接收器:
IntentFilter broadcastIntentFilter = new IntentFilter();
broadcastIntentFilter.addAction(StreamService.BROADCAST_INTENT);
LocalBroadcastManager.getInstance(context).registerReceiver((broadcastReceiver), broadcastIntentFilter);
并在解除绑定的地方取消注册 Service
:
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");
}
}