The information that there are two ways to start a Service, their impact on the Service Lifecycle is not the same.
1 through startService
Service will experience onCreate -> onStart
stopService when direct onDestroy
If the caller does not own exit and then call stopService, Service will run in the background. The caller can rise again next stopService.
2 by bindService
Service will run onCreate, this time the caller and bind together
ServicesThe caller exited, Service will call onUnbind-> onDestroyed, called a bind together to perish. And this way you can also make the caller (for example) to call other methods on the service.
Regarding point 2, said the caller to exit, then the Service will call onUnbind-> onDestroyed. I wrote a demo to see the life cycle of discovery is not the case, activity quit calling, service and does not call onDestroyed, still running, which is why?
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Post code, the three kinds of honest services, and I am only in a AIDL, simply type bindings really have not tried the code you posted, I Analysis
Reply:
bind way to start the service, the activity exits, service can continue to run.
Reply:
I also think that the document which says there is a problem.
exit when activity does not affect the Service (created by bind mode) is running, it will not automatically call UnbindService, On the contrary, if you manually invoke UnbindService, would give the Service crashes.
Reply:
After all, as the landlord posted a lot of information as to say, bound to start Service (), then with the total dead. However, if, before biand have startService (), that will be until, unbindService () and stoptService () will be terminated after Service
Reply:
Activity is not canceled this method did not write it
boundunbindService (_connection);
bindService start this way, press the Home key on the phone is not going to quit Activity, click again to come and will not perform any binding method onStart he is not executed, but according to back then, Activity will exit, Service call onUnbind -> onDestroy
Reply:
Activity in the cancellation of the binding method did not write what is meant by the activity of the code I posted:
private ServiceConnection mServiceConnection = new ServiceConnection () {
public void onServiceConnected (ComponentName name, IBinder service)
{
mMyService = ((MyService.MyBinder) service) getService ();.
mTextView.setText ("I am from Service:" + mMyService.getSystemTime ());
}
public void onServiceDisconnected (ComponentName name)
{
}
};
@ Override
public void onCreate (Bundle saveInstanceState)
{
super.onCreate (saveInstanceState);
setContentView (R.layout.main);
setupViews ();
Log.e (TAG, "start onCreate ...");
. Toast.makeText (getBaseContext (), "Activity onCreate", Toast.LENGTH_SHORT) show ();
}
@ Override
public void onStart ()
{
super.onStart ();
Log.e (TAG, "start onStart ...");
. Toast.makeText (getBaseContext (), "Activity onStart", Toast.LENGTH_SHORT) show ();
}
@ Override
public void onResume ()
{
super.onResume ();
Log.e (TAG, "start onResume ...");
. Toast.makeText (getBaseContext (), "Activity onResume", Toast.LENGTH_SHORT) show ();
}
@ Override
public void onPause ()
{
super.onPause ();
Log.e (TAG, "start onPause ...");
. Toast.makeText (getBaseContext (), "Activity onPause", Toast.LENGTH_SHORT) show ();
}
@ Override
public void onStop ()
{
super.onStop ();
Log.e (TAG, "start onStop ...");
Toast.makeText (getBaseContext (), "Activity onStop", Toast.LENGTH_SHORT) show ();.
}
@ Override
public void onDestroy ()
{
super.onDestroy ();
Log.e (TAG, "start onDestroy ...");
Toast.makeText (getBaseContext (), "Activity onDestroy", Toast.LENGTH_SHORT) show ();.
}
public void setupViews ()
{
mContext = ServiceLifeCycle.this; / / By debug, found mContext id = 8300604484368, similar to the handle
Win32mTextView = (TextView) findViewById (R.id.text);
startServiceButton = (Button) findViewById (R.id.startservice);
stopServiceButton = (Button) findViewById (R.id.stopservice);
bindServiceButton = (Button) findViewById (R.id.bindservice);
unbindServiceButton = (Button) findViewById (R.id.unbindservice);
startServiceButton.setOnClickListener (this);
stopServiceButton.setOnClickListener (this);
bindServiceButton.setOnClickListener (this);
unbindServiceButton.setOnClickListener (this);
}
public void onClick (View v) / / this method is also a registered event, the general wording directly written on the back of the setOnClickListener here alone
can also write out the{
if (v == startServiceButton)
{
/ / Intent i = new Intent ();
/ / I.setClass (ServiceLifeCycle.this, MyService.class); / / Jump to Service
from Activity/ / MContext.startService (i);
/ / StartService (i); Here you can directly do not mContext to adjust, what is the difference between the two
/ / More code and a code equivalent
belowstartService (new Intent (this, MyService.class));
}
else if (v == stopServiceButton)
{
Intent i = new Intent ();
i.setClass (ServiceLifeCycle.this, MyService.class);
mContext.stopService (i);
}
else if (v == bindServiceButton)
{
Intent i = new Intent ();
i.setClass (ServiceLifeCycle.this, MyService.class);
mContext.bindService (i, mServiceConnection, BIND_AUTO_CREATE);
}
else
{
mContext.unbindService (mServiceConnection);
}
}
Reply:
Reply:
It seems that this problem still need the actual verification, the time of exit activtiy, in the end will not be executed onUnbind, onDestroy.
Reply:
I tried pressing Back then, Service calls onUnbind, but if you use the phone in the Task Manager directly remove the program, they will not be transferred onUnbind
No comments:
Post a Comment