Android中的一个服务多个活动
我有一项服务和3项活动。服务在第一个活动中启动。 现在,当我转到第二个活动并按下某个按钮时,我想要 将数据发送到服务 因此,在第二个活动中,在click listener中,我执行了以下操作:Android中的一个服务多个活动,android,android-activity,service,Android,Android Activity,Service,我有一项服务和3项活动。服务在第一个活动中启动。 现在,当我转到第二个活动并按下某个按钮时,我想要 将数据发送到服务 因此,在第二个活动中,在click listener中,我执行了以下操作: Intent service = new Intent(MyService.class.getName()); service.putExtra("dataToSend",data.toString()); startService(service); 但是服务中的方法onStartComman
Intent service = new Intent(MyService.class.getName());
service.putExtra("dataToSend",data.toString());
startService(service);
但是服务中的方法onStartCommand没有被调用
另外,我希望创建此服务以处理多个活动。
我的意思是,每个活动都将能够向该服务发送数据并获取数据
从它 您好,您需要将正在运行的服务绑定到需要再次访问的活动。以下代码段可以用作参考
@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, MyService.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
};
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mBounded = false;
myService= null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
// Toast.makeText(context, "Service is connected", 1000).show();
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder) service;
myService= mLocalBinder.getServerInstance();
//now you are able to access any method in service class
}
};
问题
我想创建此服务以处理多个活动。我的意思是,每个活动都可以向此服务发送数据并从中获取数据。
您不必对
服务执行任何操作。如果不销毁服务,则服务将保持不变,这意味着您的所有活动
将能够访问其数据
问题
但是服务中的onStartCommand方法不会被调用
为什么
只要您的服务已启动,就不会每次调用:
每次客户端通过调用startService(Intent)显式启动服务时由系统调用,提供它提供的参数和表示启动请求的唯一整数令牌
请求启动给定的应用程序服务
解决方案
在方法中调用所需的事件:
在新客户机连接到服务时调用,之前通知它所有客户机都已在其onUnbind(Intent)中断开连接。仅当onUnbind(Intent)的实现被重写以返回true时,才会调用此函数
一种可能但不是最好的解决方案是使用SharedReference。
您可以将其用作从您的活动到您的服务的按摩发射机。他们打电话给您的服务时有以下意图
Intent mIntent = new Intent(this, MyService.class);
service.putExtra("dataToSend",data.toString());
startService(mIntent);
在您的情况下,您正在传递服务类名,这将成为符合您意图的操作。所以服务不会启动
每次调用startService时,onstartCommand都会调用,您可以从intent获取作为额外数据传递的数据
从服务接收数据到您的活动。您可以从服务广播数据,并在活动中注册广播
或
您可以使用绑定机制这就是我为使不同的活动从服务发送和接收数据所做的
public class SensorsActivity extends AppCompatActivity {
BluetoothService mService;
boolean mBound = false;
String valueReceived;
protected void onStart() {
super.onStart();
Log.v("STATE", "onStart() is called");
// Bind to BluetoothService
if (!mBound) {
Intent intent = new Intent(this, BluetoothService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensors);
LocalBroadcastManager.getInstance(SensorsActivity.this).registerReceiver(mMessageReceiver,
new IntentFilter("in.purelogic.simsonite.REQUEST_PROCESSED"));
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
BluetoothService.LocalBinder binder = (BluetoothService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
//BroadCastReceiver to let the MainActivity know that there's message has been recevied
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
handleMessage(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
};
//The Message that comes from the service
private void handleMessage(Intent msg) throws Exception {
Bundle data = msg.getExtras();
valueReceived = data.getString("DATA");
Log.e("recv2", valueReceived);
}
}显示服务代码以获取数据我建议使用EventBus,因为与绑定到活动或发送广播(假设服务发送数据)相比,它是迄今为止我发现的最简单的方法。如果您需要按需访问数据,则可能必须绑定到服务。不要使用启动/停止架构,而是使用绑定/取消绑定到该服务。谢谢,但是如果您注意到。。我确实有意打电话给startService。。据我所知,一旦startService被调用,onStartCommand也应该被调用。否?如果您查看答案中的链接,您将注意到startService
是一个Context
方法,因此不会调用onStartCommand
。。。了解Android应用程序中的生命周期很重要,可以知道发生了什么以及为什么会发生,但如果我使用以下方法调用此方法:“getApplicationContext().start…”它也不起作用,因为服务没有此方法,您已经通过startService(服务)调用它了代码>,请阅读文档:好的,我明白。我将再次解释我试图做什么:假设我有两个活动,活动A启动服务,现在我移动到活动B。在活动B中,我有一个按钮,当用户单击此按钮时,我想向服务发送数据。我再次调用startService,并将其作为参数。文档说,当您调用startService时,也会调用“onStartCommand”方法