Android - 我如何连续运行一个线程,一个接一个
Android - How do I continuously run a thread, one after another
所以我有下面的代码,它基本上获取初始电池电量,等待一定时间,然后在 calculateHelper 中获取结束电池电量,然后找到差异并打印它。
// Get the initial battery level
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
System.out.println("Initial battery level is: " + level);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final float batteryPctTemp0 = level / (float) scale;
final float batteryPct0 = batteryPctTemp0 * 100;
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("Wait time is " + waitTime);
Runnable r = new Runnable() {
@Override
public void run(){
calculateHelper(batteryPct0,startButton);
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
我想无限循环(直到程序退出)整个过程,以便在每个后续线程完成后,下一个线程开始,每次获取一个新的初始电池电量并将其传递给 calculateHelper 函数以计算新的区别。我不想让线程堆积起来。我一次想要一个线程。换句话说,循环需要等待线程完成才能开始另一个线程。
我这辈子都不知道该怎么做!如果我把整个事情放到一段时间里,它只会反复打开线程,导致 phone.
崩溃
如果有人能为我指出正确的方向,我将不胜感激。此外,如果需要更多代码来解决问题,只需发表评论,我会在将其添加到我的问题后立即回复。
谢谢。
感谢Whooper,我在循环中添加了这种调节执行顺序的方法。但是,由于某种原因,我的 postExecute() 方法从未被执行,也没有发生任何事情。
private class BatteryLifeTask extends AsyncTask<Void, Void, Void> {
// Member variables
Context appContext;
float batteryPct0;
Button startButton;
public BatteryLifeTask(Context context, Button start) {
super();
appContext = context;
startButton = start;
}
protected Void doInBackground(Void... params) {
// Get the initial battery level
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = appContext.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
System.out.println("Initial battery level is: " + level);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final float batteryPctTemp0 = level / (float) scale;
batteryPct0 = batteryPctTemp0 * 100;
return null;
}
protected void onPostExecute() {
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("In postExecute. waitTime is" + waitTime);
Runnable r = new Runnable() {
@Override
public void run(){
System.out.println("An interval has passed.");
calculateHelper(batteryPct0,startButton);
new BatteryLifeTask(appContext,startButton).execute();
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
}
以及我对执行方法的调用:
// Start the task loop
new BatteryLifeTask(getApplicationContext(), startButton).execute();
我发现了问题:
我忘记设置 @Override 注释,这个答案: 状态:
"If your params of onPostExecute(Param param) don't match the one you defined with extends AsyncTask<...,...,Param> and you didn't use the @Override annotation, it will never be executed and you don't get a warning from Eclipse."
所以我已将 postExecute 方法更正为:
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("In postExecute. waitTime is " + waitTime);
Runnable r = new Runnable() {
@Override
public void run(){
System.out.println("An interval has passed.");
calculateHelper(batteryPct0,startButton);
new BatteryLifeTask(appContext,startButton).execute();
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
所有问题现已解决。
尝试使用 AsyncTask。
http://developer.android.com/reference/android/os/AsyncTask.html
这样就可以在onPostExecute()被调用时再次执行任务
像这样:
private class BatteryLifeTask extends AsyncTask<Void, Void, Void> {
protected void doInBackground(Void... params) {
// Get the initial battery level
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
System.out.println("Initial battery level is: " + level);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final float batteryPctTemp0 = level / (float) scale;
final float batteryPct0 = batteryPctTemp0 * 100;
}
protected void onPostExecute() {
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
Runnable r = new Runnable() {
@Override
public void run(){
new BatteryLifeTask.execute();
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
}
请注意,此代码未经测试。但我希望它能给你一个想法:-)
所以我有下面的代码,它基本上获取初始电池电量,等待一定时间,然后在 calculateHelper 中获取结束电池电量,然后找到差异并打印它。
// Get the initial battery level
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
System.out.println("Initial battery level is: " + level);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final float batteryPctTemp0 = level / (float) scale;
final float batteryPct0 = batteryPctTemp0 * 100;
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("Wait time is " + waitTime);
Runnable r = new Runnable() {
@Override
public void run(){
calculateHelper(batteryPct0,startButton);
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
我想无限循环(直到程序退出)整个过程,以便在每个后续线程完成后,下一个线程开始,每次获取一个新的初始电池电量并将其传递给 calculateHelper 函数以计算新的区别。我不想让线程堆积起来。我一次想要一个线程。换句话说,循环需要等待线程完成才能开始另一个线程。
我这辈子都不知道该怎么做!如果我把整个事情放到一段时间里,它只会反复打开线程,导致 phone.
崩溃如果有人能为我指出正确的方向,我将不胜感激。此外,如果需要更多代码来解决问题,只需发表评论,我会在将其添加到我的问题后立即回复。
谢谢。
感谢Whooper,我在循环中添加了这种调节执行顺序的方法。但是,由于某种原因,我的 postExecute() 方法从未被执行,也没有发生任何事情。
private class BatteryLifeTask extends AsyncTask<Void, Void, Void> {
// Member variables
Context appContext;
float batteryPct0;
Button startButton;
public BatteryLifeTask(Context context, Button start) {
super();
appContext = context;
startButton = start;
}
protected Void doInBackground(Void... params) {
// Get the initial battery level
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = appContext.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
System.out.println("Initial battery level is: " + level);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final float batteryPctTemp0 = level / (float) scale;
batteryPct0 = batteryPctTemp0 * 100;
return null;
}
protected void onPostExecute() {
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("In postExecute. waitTime is" + waitTime);
Runnable r = new Runnable() {
@Override
public void run(){
System.out.println("An interval has passed.");
calculateHelper(batteryPct0,startButton);
new BatteryLifeTask(appContext,startButton).execute();
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
}
以及我对执行方法的调用:
// Start the task loop
new BatteryLifeTask(getApplicationContext(), startButton).execute();
我发现了问题:
我忘记设置 @Override 注释,这个答案: 状态:
"If your params of onPostExecute(Param param) don't match the one you defined with extends AsyncTask<...,...,Param> and you didn't use the @Override annotation, it will never be executed and you don't get a warning from Eclipse."
所以我已将 postExecute 方法更正为:
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
System.out.println("In postExecute. waitTime is " + waitTime);
Runnable r = new Runnable() {
@Override
public void run(){
System.out.println("An interval has passed.");
calculateHelper(batteryPct0,startButton);
new BatteryLifeTask(appContext,startButton).execute();
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
所有问题现已解决。
尝试使用 AsyncTask。 http://developer.android.com/reference/android/os/AsyncTask.html
这样就可以在onPostExecute()被调用时再次执行任务
像这样:
private class BatteryLifeTask extends AsyncTask<Void, Void, Void> {
protected void doInBackground(Void... params) {
// Get the initial battery level
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = this.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
System.out.println("Initial battery level is: " + level);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
final float batteryPctTemp0 = level / (float) scale;
final float batteryPct0 = batteryPctTemp0 * 100;
}
protected void onPostExecute() {
int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
Runnable r = new Runnable() {
@Override
public void run(){
new BatteryLifeTask.execute();
}
};
Handler h = new Handler();
h.postDelayed(r, waitTime);
}
}
请注意,此代码未经测试。但我希望它能给你一个想法:-)