I think I can pass a reference of an Activity which started an Started Service
. Since the service has a reference of the Activity, it may call methods of that reference.
Now if the Activity gets destroyed, but the service still running in background, what happens if the service calls a method of the reference of the activity?
You definitely should avoid storing references to activities in general. If so, you have to release it in activity's onDestroy()
, otherwise you can get memory leak. What is your reason to store reference in a service?
EDIT:
You can use toast notifications or status Bar notifications to notify the user the background work has completed. It's a common practice. If you need to communicate with an Activity
, there are several ways to accomplish it. You can bind to an Activity
, use Intents
or use AIDL
. You can see more details here .
I describe one of the options using Intents
along with ResultReceiver .
Service
runs in the main thread of the application, so if you are performing blocking operations you should start a new thread inside the service. In this case you can use IntentService which simplifies the work for you and already implements threading stuff.
I will make a custom ServiceResultReceiver
extended from ResultReceiver . Than I implement its interface in my Activity
and when starting the Service
I pass receiver object to IntentService
.
Custom ResultReceiver
:
public class ServiceResultReceiver extends ResultReceiver {
private Receiver mReceiver;
public ServiceResultReceiver(Handler handler) {
super(handler);
}
public void setReceiver(Receiver receiver) {
mReceiver = receiver;
}
public interface Receiver {
public void onReceiveResult(int resultCode, Bundle resultData);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(resultCode, resultData);
}
}
}
Your Activity
:
public class MyActivity extends Activity implements ServiceResultReceiver.Receiver {
private ServiceResultReceiver mReceiver;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mReceiver = new ServiceResultReceiver(new Handler());
mReceiver.setReceiver(this);
// starting a service
final Intent intent = new Intent(this, MyService.class);
intent.putExtra("receiver", mReceiver);
startService(intent);
}
...
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
// here you can handle data which came from your service.
Toast.makeText(this, "onReceiveResult()", Toast.LENGTH_SHORT).show();
}
Your Service
:
public class MyService extends IntentService {
...
@Override
protected void onHandleIntent(Intent intent) {
// executing your task
...
// getting the receiver and sending back data in Bundle (here in this example we are sending no data)
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
if (receiver != null) {
receiver.send(0, null);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.