1, system.exit (0) to exit the contents of a process. If there is a thread in an activity so that he could exit. But in this activity if you start a service, and then start a thread in the service, then he can not shut down the service and this thread. This is also why the service is not started this process yet?
Service Code
public class TestService extends Service {
private boolean isCur = true;
@ Override
public IBinder onBind (Intent intent) {
/ / TODO Auto-generated method stub
return null;
}
@ Override
public void onCreate () {
/ / TODO Auto-generated method stub
super.onCreate ();
new Thread () {
public void run () {
while (isCur) {
System.out.println ("print out ------------------------------------");
try {
Thread.sleep (5000);
} Catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
};
.} Start ();
}
@ Override
public void onDestroy () {
/ / TODO Auto-generated method stub
isCur = false;
System.out.println ("close ------------------------------------------ --------------- ");
super.onDestroy ();
}
}
/ / Activity code
public class MainActivity extends Activity {
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
}
public void close (View view) {
System.exit (0);
}
public void startSer (View view) {
Intent intent = new Intent ("analisis.resplay.voice.testservice");
startService (intent);
}
@ Override
protected void onPause () {
/ / TODO Auto-generated method stub
super.onPause ();
}
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}
}
Online seek to explain the great God
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
System.exit (0); close the main thread, the service to start another thread on the main thread does not automatically turn off after a thread to finish executing. In fact, your question is a conclusion.
Reply:
My question is actually system.extit (0) can close those things, why he can not shut down the service, the service is run in the main thread. And the same process, but he did not shut down.
Reply:
In Java, you can use this method to end the whole application in android, this method can also be turned off Activity, but the kind of situation needs attention: the service if it is open and Activity bound together, when the Activity is kill off service also will be to kill off; If the service does not bind together and Activity, when the Activity is to kill off, service was not immediately kill off, I do not know LZ can understand.
Reply:
that is, if it is bindService service will only be activated if it is by intent KILL out the need to manually stop start
Reply:
Meaning of existence is service running in the background, if it will be destroyed along with the main thread that their meaning is basically gone! If you want to close it manually in ondestory inside!
Reply:
I know this conclusion, but also have done, but now understand clearly.
system.exit (0) off the virtual machine memory, that is, kill off the current process. And why start the service will not be killed because of a service is the default start is to start a process to run. So with such a conclusion is not all of the activity or service they are set in the same process if it can system.exit (0) completely out of the system? Attempt
Reply:
I do not understand, service and activity has always been in the same thread, activity hung up, should have hung up the fishes service, but did not. . .
Reply:
After the activity if not bind together with the service, then call system.exit (0) in the activity, the entire process really quit immediately, but you carefully watch what log system, there will be a schedule restart your yingyongming 5000ms later, that is, said the system will reboot your service, you can be a test test
No comments:
Post a Comment