android - 无法在未在 CountDownTimer 中调用 Looper.prepare() 的线程内创建处理程序

标签 android service countdown

我有一个服务。还有一个方法叫做 onServiceUpdate()。此方法类似于 Google Maps API 中的 onLocationChanged()。

所以我想在 onServiceUpdate() 方法中启动 CountDownTimer 但显示如下错误:

Can't create handler inside thread that has not called Looper.prepare()
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:200)
            at android.os.Handler.<init>(Handler.java:114)
            at android.os.CountDownTimer$1.<init>(CountDownTimer.java:114)
            at android.os.CountDownTimer.<init>(CountDownTimer.java:114)
            at skripsi.ubm.studenttracking.Service2$6.<init>(Service2.java:317)
            at skripsi.ubm.studenttracking.Service2.onServiceUpdate(Service2.java:317)

这是我的代码:

    @Override
    public void onServiceUpdate(ServiceState state)
    {
final float[] distance = new float[2];
        Location.distanceBetween(state.getGeoPoint().getLatitude(), state.getGeoPoint().getLongitude(), 6.130607787619352,106.81839518499267, distance);
        if (distance[0] > 25.0)
        {

                        CountDownTimer cdt5  = new CountDownTimer(total_onServiceUpdate,1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                total_onServiceUpdate = millisUntilFinished/1000;
                            }

                            @Override
                            public void onFinish() {
                                sendSMS();
                                stopSelf();
                            }
                        }.start();

        }

最佳答案

onServiceUpdate() 是一个运行并通知您的非同步任务,因此它是一个后台线程。您需要做的就是调用 timer.start();从主线程开始,服务实际上在主线程上运行,但 intentService 不是这样,您的解决方案是

new Handler(Looper.getMainLooper()).post(new Runnable() {           
        @Override
        public void run() {
            CountDownTimer cdt5  = new CountDownTimer(total_onServiceUpdate,1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            total_onServiceUpdate = millisUntilFinished/1000;
                        }

                        @Override
                        public void onFinish() {
                            sendSMS();
                            stopSelf();
                        }
                    }.start();
        }
    });

现在您可以继续了,先生。始终调用代码与屏幕调情 main Looper

希望对你有帮助

关于android - 无法在未在 CountDownTimer 中调用 Looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32283811/

相关文章:

javascript - 为什么倒计时卡在 "1"?

android - 广告数据太大 Eddystone Beacon

java - 从静态内部类调用非静态方法

android - 如何在 AVD 管理器中添加 Nexus 9?

android - 在 Android 上获取 MYSQL 更新通知

windows - 如何手动创建 Apache Windows 服务

java - 无论操作系统如何,用 Java 执行计划任务的最佳解决方案是什么?

android - NotifyDatasetChanged 从服务 Android

MySQL 日期时间格式的 JavaScript 倒计时

java - CountdownTimer 崩溃应用程序。尝试更新 UI 线程