Home > Mobile >  How code is running in the service?
How code is running in the service?

Time:11-18

Public class MyService extends the Service {

Public static final String TAG="MyService";

Private MyBinder mBinder=new MyBinder ();

Public void haha () {
Super. The onCreate ();
The d (TAG, "onCreate () is executed");
}

@ Override
Public void onCreate () {
The common method of//service
}

@ Override
Public int onStartCommand (Intent Intent, int flags, int startId) {
The d (TAG, "onStartCommand () is executed");
Return super. OnStartCommand (intent, flags, startId);
}

@ Override
Public void onDestroy () {
Super. OnDestroy ();
The d (TAG, "onDestroy () is executed");
}

@ Override
Public IBinder onBind (Intent Intent) {
Return mBinder;
}

The class MyBinder extends Binder {

Public void startDownload () {
The d (" TAG ", "startDownload executed ()");
//download specific missions
}

}

}

When binding in the Activity of the service after return MyBinder instance, if the call MyBinder own public common methods such as tartDownload (), it is running in the service? This and direct new a MyBinder instance to run it tartDownload () what's the difference? Because it is through the service to return?

If I return through service MyBinder further returns the instance of the service, the common method of call service haha (), is this code to run in the service?


What is the code to run in the service?

CodePudding user response:

First of all, you get MyBinder and Service to create the object is not the same, this you can print proof,
Second, you get MyBinder is Service objects created by the proxy object, you invoke the MyBinder, it then calls the Service objects created

CodePudding user response:

Your new a service, he didn't start running, or bindService startService he is needed to run the onCreate

CodePudding user response:

Activity because left at the front desk is easy to be recycled, the Service lifetime of many in the background, so need long time running the code will be placed in Service operation, where in the code in the Service, is running, in the Service in the onStartCommand () or onBind () operation is in the life cycle?

CodePudding user response:

Every time onStartCommand () or bindService startService will be called once, the onCreate just call, when the first time you start to see where you need to put, or write a method directly, access to the service in the activity instance and then call,

https://blog.csdn.net/qq_19431333/article/details/53784734
Deep understanding of the Service (a) - Service lifecycle

CodePudding user response:

Feel the original poster is in service do not know ah, upstairs said is very good, recommended to take a look at the service of explanation

CodePudding user response:

Say what time I understand:

1. The Service and the Activity of four major components, they should be our instantiated, but through startActivity or startService (mainly activityManagerService) instantiated by the system and management, in different circumstances, will be invoked by the system life cycle,
2. As long as the service by providing starts, we code on any of his a life cycle is running in the service, for example, the onCreate, onbind and onStartCommand, etc.,
3. If your new a service in the activity, and then call him, the method of the object can only be regarded as an activity of normal operation, lost the value of the service,

You can refer to analogy are easier to understand the operation of the Thead, and
If you are new to a thread, directly call his run, that the thread does not start up, just an ordinary object method calls,
And you just start, this thread will take the initiative to call the run, this thread is really started,
  • Related