Android Can';t在CountDownTimer中未调用Looper.prepare()的线程内创建处理程序

Android Can';t在CountDownTimer中未调用Looper.prepare()的线程内创建处理程序,android,service,countdown,Android,Service,Countdown,我有服务。还有一个名为onServiceUpdate()的方法。此方法与Google Maps API中的onLocationChanged()类似 所以我想在onServiceUpdate()方法中启动倒计时,但显示如下错误: Can't create handler inside thread that has not called Looper.prepare() java.lang.RuntimeException: Can't create handler inside thr

我有服务。还有一个名为onServiceUpdate()的方法。此方法与Google Maps API中的onLocationChanged()类似

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

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)

我认为问题在于,在sendSMS()中,您试图执行一些需要UIThread的操作(例如更新视图)。 试试这个:

Handler mHandler = new Handler() {
 public void handleMessage(Message msg){
    sendSMS();
}};
将onFinish方法修改为

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

无法通过处理程序从服务更新活动gui,因为必须在gui线程中创建该处理程序

相反,您必须从您的服务开始,在获取此广播的活动中实现一个本地线程。onServiceUpdate()是一个运行并通知您的AYSN同步任务,因此它是一个后台线程。您只需调用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();
        }
    });
现在你可以继续了,先生。始终使用屏幕上的调情代码

希望它能帮助Android:

请在调用处理程序之前尝试此操作


如果(Looper.MyLooper()==null){Looper.Prepare();}

cdt.start()之后抛出错误。它不会在
onFinish()
方法中执行任何操作。我认为问题可能在于在每个
onTick
中更改
millisInFuture
的值,这非常有帮助。还有一个问题。我想在该处理程序中停止self(),但它不起作用。你能帮帮我吗?服务没有停止?我们可以尝试
TheNameOfYourService.this.stopSelf()
如果没有,请尝试
stopService(newintent(getApplicationContext(),TheNameOfYourService.class))循环器
?@Elltz很抱歉把问题写得不好。但我的意思是在
循环
的每次迭代中创建新的
循环器
。这对性能有多差?
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();
        }
    });