在Android中执行handler.postdayed时如何删除冻结UI?
我需要在每10秒后展示一个祝酒词,这样我就可以使用在Android中执行handler.postdayed时如何删除冻结UI?,android,multithreading,Android,Multithreading,我需要在每10秒后展示一个祝酒词,这样我就可以使用handler.postDelayed线程来解决这个问题。但是这个线程冻结用户界面1-2秒,那么我如何才能删除冻结用户界面 这是我的代码: Runnable Runnable=new Runnable(){ @凌驾 公开募捐{ gps=新轨迹gps(本); if(gps.canGetLocation()){ 经度=gps.getLongitude(); 纬度=gps.getLatitude(); Geocoder Geocoder=新的Geoco
handler.postDelayed
线程来解决这个问题。但是这个线程冻结用户界面1-2秒,那么我如何才能删除冻结用户界面
这是我的代码:
Runnable Runnable=new Runnable(){
@凌驾
公开募捐{
gps=新轨迹gps(本);
if(gps.canGetLocation()){
经度=gps.getLongitude();
纬度=gps.getLatitude();
Geocoder Geocoder=新的Geocoder(这个,Locale.getDefault());
列表地址=geocoder.getFromLocation(纬度,经度,1);
字符串地址=地址.get(0).getAddressLine(0);
mTextView.setText(地址+“\n”+“纬度:”+纬度+“经度:”+longit乌德);
}
handler.postDelayed(这是10000);
}
};
Handler=newhandler();
handler.postDelayed(可运行,10000);
这里有一个活动
类,它显示了您应该在哪里声明和初始化一些字段,以及如何使用异步任务
在后台线程中运行geocoder。代码未按原样编译,因为原始代码段缺少某些类型和声明
公共类MainActivity扩展活动{
私人经理人;
私人可运行的mRunnable;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//初始化其他东西
mHandler=新处理程序(Looper.getMainLooper());
mRunnable=newrunnable(){
@凌驾
公开募捐{
新建LocationAsyncTask().execute();
}
};
mHandler.postDelayed(mRunnable,10000);
}
私有类LocationAsyncTask扩展了AsyncTask{
@凌驾
受保护字符串[]doInBackground(Void…params){
gps=新轨迹gps(本);
if(gps1.canGetLocation()){
经度=gps.getLongitude();
纬度=gps.getLatitude();
Geocoder Geocoder=新的Geocoder(MainActivity.this,Locale.getDefault());
列表地址=geocoder.getFromLocation(纬度,经度,1);
字符串地址=地址.get(0).getAddressLine(0);
字符串[]字符串=新字符串[3];
字符串[0]=地址;
字符串[1]=纬度;
字符串[2]=经度;
返回字符串;
}否则{
返回null;
}
}
@凌驾
受保护的void onPostExecute(字符串[]字符串){
super.onPostExecute(字符串);
if(字符串!=null){
updateText(字符串[0],字符串[1],字符串[2]);
}
mHandler.postDelayed(mRunnable,10000);
}
}
}
我只看到一个线程。可能是其他原因冻结了UIC,但是这个线程每10秒由handler.postDelayed(runnable,10000)重复一次;我明白你的意思,但要小心,这不是线程的正确定义。而且,您提供的代码没有编译,因此请提供您实际使用的代码Runnable Runnable=new Runnable(){@Override public void run(){gps=new TrackGPS(此);if(gps1.canGetLocation()){longitude=gps.getLongitude();lation=gps.getLatitude();Geocoder Geocoder=new Geocoder(此,Locale.getDefault())列表地址=地理编码器。getFromLocation(纬度,经度,1);字符串地址=地址。get(0)。getAddressLine(0);mTextView。setText(地址+“\n”+“Lat:”+纬度+“Lon:”+经度);}handler.postDelayed(这个,10000);}代码>考虑使用代码更新您的问题。处理程序
声明也很重要。它将有助于提供您定义的所有符号,例如,我需要知道什么是TrackGPS
类,以及gps1
@nandsite的类型,感谢它的出色工作。如果我可以在使用mTextView.setText(地址+“\n”+“Lat:”+纬度+“Lon:”+经度)的地方执行函数代码>带有updateText(地址、纬度、经度)代码>那么我怎么能做到这一点。@ShivamKumar我不明白你的评论,什么是updateText()
?您定义的方法?&函数体是updateText(地址、纬度、经度){mTextView.setText(地址+“\n”+”Lat:“+纬度+”Lon:“+经度);}
@ShivamKumar您可以使用此方法代替mTextView.setText()
没有问题,但我如何区分地址、纬度和经度。因为我们得到了s(字符串)中的返回值。