我对Android有点新,所以请帮助我理解这一点。 在从Android开发者网站上阅读服务时,我发现这段我无法想出一个例子:
如果服务不提供绑定,则startService()提供的意图是应用程序组件与服务之间唯一的通信方式。但是,如果您希望服务返回结果,则启动服务的客户端可以为广播创建PendingIntent(使用getBroadcast())并将其传递给启动服务的Intent中的服务。然后,该服务可以使用广播来传递结果。
我知道还有其他方法可以将结果发送回活动,但我看到上面解释的逻辑比任何其他方式都简单。请通过一个例子帮助我理解这一点。
答案 0 :(得分:0)
我们可以使用BroadcastReceiver
或ResultReceiver
来接收Service
的结果。在这段时间里,我想引导您使用ResultReceiver
:
首先,创建一个类以在活动中接收结果并扩展ResultReceiver
:
private class MyReceiver extends ResultReceiver {
public MyReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
/*
* We can get the type of data passed from Service to Activity:
* int --> getInt("key")
* String --> getString("key")
* boolean --> getBoolean("key")
* /
if (resultCode == MyService.RUNNING) {
String str = resultData.getString("dataStringKey"); // get information within the key, i.e. "dataString"
myTextView.setText(str); // set the text to get the data received
}
if (resultCode == MyService.SERVICE_STOPPED) {
// do something, such as get data received with the key
}
}
}
在Service
:
public static final int RUNNING = 0;
public static final int SERVICE_DESTROYED = 1;
private ResultReceiver receiver;
private Bundle resultData;
private int status;
@Override
public void onCreate() {
super.onCreate();
resultData = new Bundle();
status = RUNNING;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
receiver = intent.getParcelableExtra("receiver");
String dataFromActivity = intent.getStringExtra("text");
/*
* send result/information to activity, also we can put:
* int --> putInt("key", value)
* boolean --> putBoolean("key", value)
* String --> putString("key", "My string.")
* /
resultData.putString("dataStringKey", "Hi, this is a String data.");
receiver.send(status, resultData);
return Service.START_NOT_STICKY;
}
@Override
public void onDestroy() {
status = SERVICE_DESTROYED;
receiver.send(status, resultData);
super.onDestroy();
}
当您触发Service
:
Intent intent = new Intent(MyActivity.this, MyService.class);
intent.putExtra("text", "Text to be passed to Service.");
intent.putExtra("receiver", new MyReceiver(new Handler()));
this.startService(intent);
答案 1 :(得分:0)
使用Intent
将数据从Activity
发送到Service
,并使用LocalBroadcastManager将数据从您的服务发送到活动。
这是关于如何使用LocalBroadcastManager的example。
public class LocalBroadcastExampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.log_list);
Button buttonStartService = (Button)findViewById(R.id.button_ok);
buttonStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Register MessageService in Manifest to work
Intent intent = new Intent(LocalBroadcastExampleActivity.this, MessageService.class);
intent.putExtra("mydatakey", myString); // <--- set data to send to service
startService(new Intent(LocalBroadcastExampleActivity.this, MessageService.class));
}
});
}
@Override
protected void onPause() {
// Unregister since the activity is paused.
LocalBroadcastManager.getInstance(this).unregisterReceiver(
mMessageReceiver);
super.onPause();
}
@Override
protected void onResume() {
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter("custom-event-name"));
super.onResume();
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
public class MessageService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
String mydata = intent.getStringExtra("mydatakey"); // <--- retrieve data sent from Service
sendMessage();
return super.onStartCommand(intent, flags, startId);
}
// Send an Intent with an action named "custom-event-name". The Intent
// sent should
// be received by the ReceiverActivity.
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
}