gpt4 book ai didi

java - AsyncTask 不会延迟 Handler 中的操作

转载 作者:行者123 更新时间:2023-12-02 11:14:00 26 4
gpt4 key购买 nike

我正在编写应用程序,我想从 Service 中初始化的 BroadCastReceiver 调用 AsyncTask。我想让 AsyncTask 在后台延迟执行一些工作,但处理程序似乎不起作用,因为我在 Logcat 中得到此输出(简而言之):

05-18 20:33:01.396 19382-19382// D/AsyncTask: Sleeping for a while
Sleeping for a while
.
.
05-18 20:33:01.406 19382-19382// D/AsyncTask: Sleeping for a while
Sleeping for a while
.
.

这是我的代码:

示例服务.java:

import java.util.*;
import android.content.*;
import com.google.firebase.messaging.*;
import timber.log.*;

public class ExampleService extends FirebaseMessagingService {

private AnotherExample anotherExample;

@Override
public void onMessageReceived(final RemoteMessage message) {
Timber.d("onMessageReceived(): message: %s", message);

anotherExample = new AnotherExample();
if (message.getData() != null) {
processMessage(message.getData());
}
}

private void processMessage(Map<String, String> data) {
Timber.d("processMessage(): data: %s", data);

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Timber.d("onReceiveScreenOff()");
anotherExample.create();
anotherExample.resume();
}
}, filter);

IntentFilter filter1 = new IntentFilter();
filter1.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Timber.d("onReceiveScreenOn()");
anotherExample.suspend();
}
}, filter1);
}
}

AnotherExample.java

public class AnotherExample {

AsyncTask asyncTask;

public void create() {
this.asyncTask = new MyAsyncTask();
}

public void resume() {
this.asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

public void suspend() {
this.asyncTask.cancel(true);
}
}

MyAsyncTask.java

public class MyAsyncTask extends AsyncTask {

private Handler handler;
private Runnable runnable;

private void setupRunnable() {
runnable = new Runnable() {
@Override
public void run() {
Log.d("AsyncTask", "Sleeping for a while");
}
};
}

@Override
protected Object doInBackground(Object[] objects) {
while (!isCancelled()) {
handler.postDelayed(runnable, 1000);
}

return null;
}

@Override
protected void onCancelled() {
super.onCancelled();
Log.d("AsyncTask", "Removing callbacks");
handler.removeCallbacks(runnable);
}
}

不知道问题出在哪里,我也用Thread.sleep(1000)尝试过,但结果是一样的。

还有其他方法可以实现吗?

另外我想问一下,AsyncTask和BroadCastReceiver使用同一个线程吗?因为看起来 BroadCastReceiver 在屏幕打开时无法获取信息。

感谢您的每一个建议。

最佳答案

我无法重现您的问题。我的代码似乎运行良好。

只需这样称呼:

private void testAsyncTaskDelay(){
TestEventsData test = new TestEventsData();
test.execute();
}

我的TestEventsData类定义:

class TestEventsData extends AsyncTask<String, Integer, Boolean> {
Handler testHandler = new Handler();

@Override
protected Boolean doInBackground(String... params) {
Boolean success = false;
try {
long startTime = SystemClock.elapsedRealtime();
Log.e(TAG, "doInBackground Start " + startTime);

testHandler.postDelayed(new Runnable() {
@Override
public void run() {
long endTime = SystemClock.elapsedRealtime();
long diff = endTime - startTime;
Log.e(TAG, "run Start " + endTime);
Log.e(TAG, "run Start diff " + diff);

}
}, 3000);

}
catch (Exception ex) {
Log.e(TAG, "doInBackground --- " + ex.getMessage());
}

return success;
}


@Override
protected void onCancelled() {
}

@Override
protected void onPostExecute(Boolean success) {
try{
Log.e(TAG, "onPostExecute NOW");
}
catch(Exception ex){
Log.e(TAG, "onPostExecute" + ex.getMessage());
}
}

@Override
protected void onPreExecute() {
super.onPreExecute();
}
}

我的 logcat 输出:

05-18 15:15:51.951 24329-24345/? E/MainActivity: doInBackground Start 1471880721 
05-18 15:15:52.003 24329-24329/? E/MainActivity: onPostExecute NOW
05-18 15:15:54.953 24329-24329/com.asb.android.mcc E/MainActivity: run Start 1471883724
05-18 15:15:54.953 24329-24329/com.asb.android.mcc E/MainActivity: run Start diff 3003

时差3003ms!在我的 postDelayed() 中设置相同的时间。

关于java - AsyncTask 不会延迟 Handler 中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50417686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com