An application has more than one activity, some activity defined thread. When you press the return key, this thread will stop it.
I want to be as long as there is no exit the current application, the thread has been running.
Trouble with a master solvable help solve. Thank you! ^ _ ^<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Service can only be used to do, Activity was kill, and all the stuff inside the naturally also kill it.
Reply:
Before you be such an example?
What to do? ? ? ? Trouble introduce some ......
Reply:
activity to finish off, thread will not necessarily kill. But you can not find a reference to his right, and as HandlerThread. You do not quit, it will have been.
If you want to be a thread has been running, you can start Thread on Application of the method onCreate let him do things has been circulating.
If you want to use the service is also possible. It is an important component, but does not guarantee the service has been running, maybe at some point the system will kill it, even if you set it to foreground service
Reply:
Then how to exit the app stop sevice it?
Please advise! ! !
Reply:
AIDL achieved through the application of communications and service before,
For example there is a loop in the service to ensure that service has been running
For example, the definition of a global variable object hasRun service to determine whether to continue running
private boolean hasRun = true;
....
while (hasRun)
{
.....
}
When you want to end, by modifying hasRun = false to conclude, therefore need to write on the service:
public void setFinish ()
{
hasRun = false;
}
If you do not come into contact with the landlord before AIDL and service, it is recommended to go to familiarize yourself with.
Reply:
Stop service. Use stopService chant.
Reply:
Complete process code is as follows:
package com.pb.demo;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ThreadRunningDemoActivity extends Activity {
private Button btn;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
btn = (Button) findViewById (R.id.button1);
btn.setOnClickListener (btnListener);
}
private OnClickListener btnListener = new OnClickListener () {
@ Override
public void onClick (View v) {
Intent intent = new Intent ();
intent.setClass (getApplication (), SecondActivity.class);
startActivity (intent);
}
};
@ Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
/ / Kill Application
ActivityManager activityMgr = (ActivityManager) this
. GetSystemService (ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses (getPackageName ());
android.os.Process.killProcess (android.os.Process.myPid ());
new MyService () stopSelf ();.
/ * If (getApplicationContext (). StopService (new Intent (
ThreadRunningDemoActivity.this,
MyService.class)))
Log.v ("FirstActivity", "stopService was successful");
else
Log.v ("FirstActivity", "stopService was unsuccessful"); * /
}
return super.onKeyDown (keyCode, event);
}
}
package com.pb.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
private Handler mHandler;
private Button btn;
int count = 10;
TextView tv;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.second);
btn = (Button) findViewById (R.id.second_btn);
tv = (TextView) findViewById (R.id.text);
btn.setOnClickListener (btnListener);
mHandler = new Handler ();
checkUpdate.start ();
}
private OnClickListener btnListener = new OnClickListener () {
@ Override
public void onClick (View v) {
Intent intent = new Intent ();
intent.setClass (getApplicationContext (), MyService.class);
startService (intent);
}
};
private Thread checkUpdate = new Thread () {
public void run () {
mHandler.post (showUpdate);
}
};
private Runnable showUpdate = new Runnable () {
public void run () {
if (count> = 1) {
tv.setText ("" + count);
} Else {
count = 10;
tv.setText ("" + count);
}
count -;
mHandler.postDelayed (showUpdate, 1000);
}
};
}
AndroidManifest.xml as follows:
android: versionCode = "1"
android: versionName = "1.0">
android: label = "@ string / app_name">
android: label = "@ string / app_name">
package com.pb.demo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
public class MyService extends Service {
private Handler mHandler = new Handler ();
private static int count = 1;
/ / Define a label
a Tagprivate static final String TAG = "MyService";
/ / Here it a Binder class definitions used in onBind () method in there, so there can get to
Activityprivate MyBinder mBinder = new MyBinder ();
@ Override
public IBinder onBind (Intent intent) {
Log.v (TAG, "start IBinder ~ ~ ~");
return mBinder;
}
@ Override
public void onCreate () {
myThread.start ();
Log.v (TAG, "start onCreate ~ ~ ~");
super.onCreate ();
}
@ Override
public void onStart (Intent intent, int startId) {
Log.v (TAG, "start onStart ~ ~ ~");
super.onStart (intent, startId);
}
@ Override
public void onDestroy () {
Log.v (TAG, "start onDestroy ~ ~ ~");
super.onDestroy ();
}
@ Override
public boolean onUnbind (Intent intent) {
Log.v (TAG, "start onUnbind ~ ~ ~");
return super.onUnbind (intent);
}
/ / Here I wrote a function to get the current time, but it is not formatted so the bar on the first
public String getSystemTime () {
Time t = new Time ();
t.setToNow ();
return t.toString ();
}
public class MyBinder extends Binder {
MyService getService () {
return MyService.this;
}
}
private Thread myThread = new Thread () {
@ Override
public void run () {
mHandler.post (updateData);
}
};
private Runnable updateData = new Runnable () {
@ Override
public void run () {
Log.v ("count", "" + count);
count + +;
mHandler.postDelayed (updateData, 1000);
}
};
}
Reply:
This is what I wrote a demo. After start service, I return to the first activity, click the back button to exit the app and use stop service. In logcat where you can see the following: about 3 seconds pause time service does not start automatically three seconds after the service has started. Service did not stop off at this time. Trouble insider commentary to the next, thank you! ^ _ ^
Reply:
1.service is a system to create, and you do not new. You want to stop him, with stopService (context, MyService).
But if someone behind bind or start it, it will be up. And even if you kill it in the process, once the service starts, acvitivymanagersservice to detect whether the process would have to start, no one on the first fork, then go createserice.
2 best not themselves stop process. Process list is system maintenance.
Reply:
I commented that the use of context.stopService (contenxt, Myservice.class). Or useless.
I do not stop system app, do I want to customize the Application class?
Under the guidance of trouble you, thank you! ^ _ ^
Reply:
Do not stop away, you have applications still use him,
You still have to be made as long as the application, thread has been running, then it would inherit the Application class look.
That this thread has been running, unless the process to kill.
Reply:
It stands to reason I quit the app applications, thread how it still running?
Reply:
What is your exit app concept? Han died process it? If you kill the process, the thread does not exist.
If the exit is the activity, activity in a activitymanagerservice maintained in ActivityThread also has a clientrecode, if you quit, there is no record of these two, but this does not matter and thread, the thread loop if there are, or have looper, will always, unless they quit, or to kill
Reply:
That is, after exit app, you should stop service! This issue has been resolved!
Reply:
About onKeyDown exit the system when problems stopService see you have encountered similar problems, I do not know how to solve it? Neighborhoods
No comments:
Post a Comment