Android中定时器的学习总结

时间:2021-10-17 23:23:51

 直接上一段代码,当然其中还有很多无用的代码,定时器怎么用,我已用红色标识,已经很清楚了,当然这并不是一个完整的Demo,仅供参考Timer的用法:

   
public class QuoteRankingListActivity extends Activity  {


int m_sortType = DataLayer.SortType_None;
int m_Offset = 0;
boolean m_SortDesc = true;
public int m_listType;
private Handler mHandler;
private int m_nActivePage = 0;
private int m_nTotalPage = 0;
private ListView mStockListView = null;
public String ActivityKind;
private int stocklist_count = 10;
private ProgressDialog mProgDialog;
ListStockAdapter m_listAdapter;
List<QuoteData> m_ListQuote = new ArrayList<QuoteData>();
Timer timer;
TimerTask task;


public void setTitle(String title) {
Message message = new Message();
message.what = 0x01010101;
message.obj = title;
if (mHandler != null) {
mHandler.sendMessage(message);
}
}


private int GetSortType() {
int sortType = m_sortType;
if (m_SortDesc)
sortType |= (1 << 16);
return sortType;
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.stock_list);
Button editbtn = (Button) findViewById(R.id.buttonEdit);
TextView v = (TextView) findViewById(R.id.myStockTitle);
editbtn.setVisibility(View.GONE);
v.setVisibility(View.GONE);
m_listType = getIntent().getIntExtra("listType", 0);
String name = getIntent().getStringExtra("name");
mStockListView = (ListView) findViewById(R.id.listView1);
mStockListView.setOnItemClickListener(this);
ActivityKind = getIntent().getStringExtra("ActivityKind");
m_nTotalPage = DataLayer.getInstance().GetStockCount(m_listType)
/ stocklist_count;
View button = null;
final int[] columns = { R.id.columnCR, R.id.columnNP, R.id.columnVol,
R.id.columnVR };
for (final int col : columns) {
button = (View) findViewById(col);
if (button != null) {
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {


try {
int column = Integer.parseInt((String) v.getTag());
if (column != m_sortType) {
m_sortType = column;
m_SortDesc = true;
} else {
m_SortDesc = !m_SortDesc;
}
m_Offset = 0;
mProgDialog = ProgressDialog.show(
QuoteRankingListActivity.this, "等待中",
"正在获取数据...", true, true);
DataLayer.getInstance().GetCodeList(m_listType,
m_Offset, stocklist_count, GetSortType(),
QuoteRankingListActivity.this);
} catch (Exception e) {
Log.e("ColumnHeaderClick", e.getMessage());
}
}
});
}
}
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x01010101) {
TextView tv = (TextView) findViewById(R.id.title_bar);
tv.setText((String) msg.obj);
} else if (msg.what == 0x01010102) {

                                       / /定时从服务器端请求列表数据
DataLayer.getInstance().GetCodeList(m_listType, m_Offset,
stocklist_count, GetSortType(),
QuoteRankingListActivity.this);
}
}
};
m_ListQuote = DataLayer.getInstance().GetCodeList(m_listType, 0, 20,
GetSortType(), QuoteRankingListActivity.this);
m_listAdapter = new ListStockAdapter(QuoteRankingListActivity.this,
R.layout.stock_list_item, m_ListQuote);
               CreateTask();
               timer=new Timer();
       timer.schedule(task, 2000, 3000);//第一次是2秒刷新,间隔3秒后在循环操作


Button queryButton = (Button) findViewById(R.id.buttonQuery);
// 点击右上方的"个股查询"所触发的点击事件
queryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(0);
}
});
if (name != null) {
String title = String.format("行情-%s", name);
setTitle(title);
} else {
setTitle("行情");
}


}
public void CreateTask(){
           task = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 0x01010102;
mHandler.sendMessage(message);
}
};


}

       //当然定时器不用的时候或者切换到别的功能页的时候,要把它给适时的关闭掉。当再次到达用到定时器的页面时,定时器开启。我以下用到的几个生命周期函数不一定非得都要用上,可以视情况而用。
protected void onPause() {
        if(timer!=null){
        timer.cancel();
        timer=null;
        }
super.onPause();
}
@Override
protected void onStop() {
        if(timer!=null){
        timer.cancel();
        timer.purge();
        timer=null;
        }


super.onStop();
}
@Override
protected void onDestroy() {
        if(timer!=null){
        timer.cancel();
        timer=null;
        }


super.onDestroy();
}
@Override
protected void onResume() {
if(timer==null)
{
CreateTask();
timer=new Timer();
timer.schedule(task, 2000, 3000);
}
super.onResume();
}
@Override
protected void onRestart() {
super.onRestart();
}
protected void moveToPage(int page) {


m_Offset = page * stocklist_count;
DataLayer.getInstance().GetCodeList(m_listType, m_Offset,
stocklist_count, GetSortType(), this);
}


public void onLeftFling() {
if (m_nActivePage == m_nTotalPage - 1)
return;
m_nActivePage += 1;
moveToPage(m_nActivePage);
}


public void onRightFling() {
if (m_nActivePage == 0)
return;
m_nActivePage -= 1;
moveToPage(m_nActivePage);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.stock_list, menu);
return true;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View v=view;
return v;
}
}

}


}