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

Can't create handler inside thread that has not called Looper.prepare() in CountDownTimer

我有一个服务。还有一个方法叫做 onServiceUpdate()。此方法与 Google 地图 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.<init>(CountDownTimer.java:114)
            at android.os.CountDownTimer.<init>(CountDownTimer.java:114)
            at skripsi.ubm.studenttracking.Service2.<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();

        }

我认为问题是在您的 sendSMS() 中您正试图做一些需要 UIThread 的事情(比如更新视图)。 试试这个:

Handler mHandler = new Handler() {
 public void handleMessage(Message msg){
    sendSMS();
}};

修改onFinish方法为

@Override
public void onFinish() {
    mHandler.sendEmptyMessage(0);
    stopSelf();
}

您不能通过处理程序从服务更新 activity gui,因为处理程序必须在 gui 线程中创建。

相反,您必须在 activity 中 send a broadcast from your service and implement a local broadcast-receiver 获得此广播

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 :

在调用处理程序之前尝试这个

if (Looper.MyLooper() == null) { Looper.Prepare(); }