使用自定义线程来更新组件中的数据
来源:互联网 发布:js array 遍历 编辑:程序博客网 时间:2024/06/05 16:06
在Android系统中,Activtiy,Service和BroadercastReceiver都是工作在主线程上,因此任何耗时的处理过程都会降低用户界面的响应速度,甚至导致用户界面失去响应。当用户界面失去响应超过5秒, Andorid系统会允许用户强行关闭应用程序。 因此,较好的解决方案是将耗时的处理过程转移到转移到子线程上,这样可以避免负责界面更新的主线程无法处理界面事件,从而避免用户界面长时间失去响应。 下面就举《Android应用程序开发》中的一个示例:
该示例的要点是通过Handler将产生的随机数显示在用户界面上。
package my.yaner.service;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**使用自定义线程来控制service将数据传递给UI控件*/
public class ThreadServiceActivity extends Activity {
private static Handler handler = new Handler();
private static TextView lableView =null;
private static double randomDouble;
Button startservice, stopservice;
protected void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
LinearLayout linearlayout = new LinearLayout(this);
lableView = new TextView(this);
startservice = new Button(this);
stopservice = new Button(this);
startservice.setText("启动service");
stopservice.setText("停止service");
lableView.setText("此处显示随机数");
linearlayout.addView(lableView);
linearlayout.addView(startservice);
linearlayout.addView(stopservice);
setContentView(linearlayout);
final Intent servieIntent = new Intent(this, RandomService.class);
startservice.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
startService(servieIntent);
}
});
stopservice.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(servieIntent);
}
});
}
public static void UpdateGUI(double refreshDouble){
randomDouble = refreshDouble;
handler.post(refreshLable);
}
private static Runnable refreshLable =new Runnable(){
public void run(){
lableView.setText(String.valueOf(randomDouble));
}
};
}
package my.yaner.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class RandomService extends Service {
private Thread workThread;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
Toast.makeText(this, "(1)调用onCreate()", Toast.LENGTH_LONG).show();
workThread = new Thread(null, backgroundWork, "WorkThread");
}
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "(3)调用onDestory()", Toast.LENGTH_SHORT).show();
workThread.interrupt();
}
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "(2)调用onStart()", Toast.LENGTH_SHORT).show();
if (!workThread.isAlive()) {
workThread.start();
}
}
private Runnable backgroundWork = new Runnable() {
@Override
public void run() {
try {
while (!Thread.interrupted()) {
double randomDouble = Math.random();
ThreadServiceActivity.UpdateGUI(randomDouble);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
<service android:name=".service.RandomService"></service>
- 使用自定义线程来更新组件中的数据
- 使用DataAdapter来更新数据
- 自定义列中的数据更新
- 关于Thread -----使用线程更新进度组件
- 线程中如何更新对话框中的数据
- Python使用线程来接收串口数据
- Android的线程使用来更新UI----Thr…
- 使用Django来清理数据库中的数据
- 使用文件来保存程序中的数据
- WinForm,在另一个线程中更新Form中的数据
- WEB自定义数据组件
- 使用 ADO.NET 来创建、读取、更新和删除数据
- ReactJS中的自定义组件
- Android组件ExpandableListView及其适配器,线程中的runOnUiThread()的使用
- ListView中使用自定义Adapter及时更新数据
- 关于ListView中使用自定义Adapter及时更新数据
- 线程自定义数据
- flex3 :自定义组件使用
- 软件工程之软件需求分析
- POJ 1062 昂贵的聘礼 最短路枚举等级限制
- poj 2463 物理水题(机械能守恒定理)
- win7+php5.2.17 文件上传出错
- java虚拟机以及JIT技术简要笔记
- 使用自定义线程来更新组件中的数据
- 如此看企业流程
- 关于线程的一些小细节
- CString 和 LPCTSTR 之间的转换 及 LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR的区分与转化
- 初学windows网络编程,编程时易出现的问题。
- 一个乞丐经济学
- Flashback Drop
- IIS6和tomcat6的整合,测试成功并域名成功解析
- 递归和迭代的区别和联系