- Java代码
- package com.ray.test;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.os.Bundle;
- public class JavaTimer extends Activity {
- Timer timer = new Timer();
- TimerTask task = new TimerTask(){
- public void run() {
- setTitle("hear me?");
- }
- };
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- timer.schedule(task, 10000);
- }
- }
- 但实际上的用法应该通过配合Handler来实现timer功能的!
- Java代码
- package com.ray.test;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- public class TestTimer extends Activity {
- Timer timer = new Timer();
- Handler handler = new Handler(){
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- setTitle("hear me?");
- break;
- }
- super.handleMessage(msg);
- }
- };
- TimerTask task = new TimerTask(){
- public void run() {
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- }
- };
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- timer.schedule(task, 10000);
- }
- }
相关文章
- 12-14在android中开发每日警报
- 12-14Android Studio中Junit单元测试使用JSON对象异常的问题
- 12-14在Android应用中播放YouTube视频
- 12-14android – 有没有办法从我的活动中播放流媒体YouTube视频?
- 12-14如何在Android中的ExoPlayer中播放Youtube视频?
- 12-14android – 在VideoView中播放流媒体,将网址转换为rtsp
- 12-14android-在AIR应用(AS3)中播放youtube视频
- 12-14Android在webview中播放youtube视频
- 12-14android-片段YouTubePlayerSupportFragment中的ID重复
- 12-14android – WebView – Youtube视频在旋转的背景中播放和最小化