您的位置:首页 > 移动开发 > Android开发

android 自动更新数据示例

2012-01-09 15:51 232 查看
package haha.android.HelloWorld3;

import java.util.InputMismatchException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HelloWorld3Activity extends Activity {

String mTitleStr="haha title:";
int mCount= 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

long mpid=Thread.currentThread().getId();
System.out.println("haha:main pid="+mpid);

th.start();

}

void Printlog(int mCount)
{

}

void mySleep(long sec)
{
try{
Thread.sleep(sec);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

void myAction(int count)
{
String tmp =mTitleStr +mCount;
this.setTitle(tmp);
}
Handler  hd = new Handler (){
public void handleMessage (Message msg)
{
super.handleMessage(msg);

if(msg.what == 111)
{
Printlog(msg.arg1);

myAction(msg.arg1);

mySleep(1000);
Message m= new Message();
m.what = 111;
m.arg1 = mCount++;
hd.sendMessage(m);
}

}
};

Thread th = new Thread(){
public  void run ()
{
Message m= new Message();
m.what = 111;
m.arg1 = mCount++;
mySleep(1000);
long tpid=Thread.currentThread().getId();
System.out.println("haha:thread pid="+tpid);
hd.sendMessage(m);
}
};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: