9.Android Handler的使用

本文探讨了在Android开发中使用Handler类实现定时更新UI元素的方法,通过引入Handler简化了传统的Java编程方式,展示了其作为Runnable和Activity交互桥梁的作用。文章详细介绍了Handler的使用场景及其实现过程,并通过实例演示了如何利用Handler实现特定功能。

大家好我们这一节讲的是Android Handler的使用,在讲Handler之前,我们先提个小问题,就是如何让程序5秒钟更新一下Title.

首先我们看一下习惯了Java编程的人,在不知道Handler的用法之前是怎么样写的程序,代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.android.tutor;
 
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
 
public class HandlerDemo extends Activity {
     // title为setTitle方法提供变量,这里为了方便我设置成了int型
     private int title = 0 ;
 
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
 
         Timer timer = new Timer();
         timer.scheduleAtFixedRate( new MyTask(), 1 , 5000 );
     }
 
     private class MyTask extends TimerTask {
         @Override
         public void run() {
 
             setTitle( "Welcome to Mr Wei's blog " + title);
             title++;
         }
     }
}

然而当我们执行程序,并不能达到我们预期的效果,所以Android引进了Handler这个特殊的类,可以说它是Runnable和Activity交互的桥梁,所以我们只要在run方法中发送Message,而在Handler里,通过不同的Message执行不同的任务。

所以我们修改后的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.android.tutor;
 
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 HandlerDemo extends Activity {
 
     // title为setTitle方法提供变量,这里为了方便我设置成了int型
     private int title = 0 ;
 
     private Handler mHandler = new Handler() {
 
         public void handleMessage(Message msg) {
             switch (msg.what) {
             case 1 :
                 updateTitle();
                 break ;
             }
         };
     };
 
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
 
         Timer timer = new Timer();
         timer.scheduleAtFixedRate( new MyTask(), 1 , 5000 );
     }
 
     private class MyTask extends TimerTask {
         @Override
         public void run() {
 
             Message message = new Message();
             message.what = 1 ;
             mHandler.sendMessage(message);
 
         }
     }
 
     public void updateTitle() {
 
         setTitle( "Welcome to Mr Wei's blog " + title);
         title++;
     }
}

下面我们看一下效果图:

HandlerDemo1

HandlerDemo2


Porcess:com.ss.android.ugc.aweme PackageName:com.ss.android.ugc.aweme versionName:360201 versionCode:360201 Foreground:true time:1762175449152 statcktrace:java.lang.NoClassDefFoundError: Invalid descriptor: Lio/reacԩvex/Observable1. at java.lang.reflect.Executable.getMethodReturnTypeInternal(Native Method) at java.lang.reflect.Method.getReturnType(Method.java:148) at java.lang.Class.getDeclaredMethods(Class.java:2730) at java.lang.reflect.Proxy.getMethodsRecursive(Proxy.java:797) at java.lang.reflect.Proxy.getMethods(Proxy.java:786) at java.lang.reflect.Proxy.-$$Nest$smgetMethods(Unknown Source:0) at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:678) at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:602) at java.lang.reflect.WeakCache$Factory.get(WeakCache.java:230) at java.lang.reflect.WeakCache.get(WeakCache.java:127) at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:438) at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:873) at com.GlobalProxyLancet.com_ss_android_ugc_aweme_lancet_ProxyLancet_newProxyInstance(SourceFile:50593851) at com.bytedance.retrofit2.Retrofit.create(SourceFile:16973849) at com.bytedance.android.live.network.LiveClient.getService(SourceFile:17039376) at com.bytedance.android.live.core.CommonUtilsServiceImpl.liveClientGetService(SourceFile:16908292) at com.bytedance.android.live.core.feature.RoomFeatureUtils.LIZIZ(SourceFile:33947718) at com.ss.android.ugc.aweme.live.feedpage.LivePartialComponentV2.handleLoadMore(SourceFile:33947754) at com.ss.android.ugc.aweme.feed.plato.core.FeedComponentGroup.handleLoadMore(SourceFile:33816616) at X.0enV.onLoadMoreResult(SourceFile:34079357) at com.ss.android.ugc.aweme.common.presenter.BaseListPresenter.onSuccess(SourceFile:393279) at com.ss.android.ugc.aweme.feed.presenter.FeedFetchPresenter.onSuccess(SourceFile:459087) at X.0Wz0.onSuccess(SourceFile:458900) at com.ss.android.ugc.aweme.mvp.TetrisBaseListModel$notifySuccessListenerHandler$1.handle(SourceFile:33816617) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.BusinessExpandAnchorDataHandle.handle(SourceFile:33751057) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.MusicCardPreloadHandler.handle(SourceFile:33751057) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.ActivityCardDataPreloadHandler.handle(SourceFile:33816599) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.MonitorDataResponseAfterHandler.handle(SourceFile:33882195) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.MobFeelGoodItemHandler.handle(SourceFile:33816605) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.FeedModelStatisticsResponseHandler.handle(SourceFile:33882212) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.mvp.TetrisBaseListModel$notifyErrorListenerHandler$1.handle(SourceFile:33882174) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed_disaster_recovery.FeedDisasterRecoveryManager$ResponseHandler.handle(SourceFile:34013330) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.autoretry.FeedRetryRequestManager$ResponseHandler.handle(SourceFile:34013327) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.autoretry.FeedNetRecoverManager$ResponseHandler.handle(SourceFile:33947694) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.handler.TetrisHandlerGroup.triggerChildren(SourceFile:33816602) at com.ss.android.ugc.aweme.handler.TetrisHandlerGroup.handle$suspendImpl(SourceFile:50724940) at com.ss.android.ugc.aweme.handler.TetrisHandlerGroup.handle(Unknown Source:0) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.handler.TetrisHandlerGroup.triggerChildren(SourceFile:33816602) at com.ss.android.ugc.aweme.handler.TetrisHandlerGroup.handle$suspendImpl(SourceFile:50724940) at com.ss.android.ugc.aweme.handler.TetrisHandlerGroup.handle(Unknown Source:0) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.FeedCacheIndexReviseHandler.handle(SourceFile:33947751) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.PrefetchBootFeedResponseHandler.handle(SourceFile:33947774) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.FeedCacheResponseHandler.handle(SourceFile:34013367) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.SnapshotFeedResponseHandler.handle(SourceFile:34013247) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.FirstFeedSplitSkipHandler.handle(SourceFile:34013497) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.FeedLiveWatchHistoryResponseHandler.handle(SourceFile:33882180) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.AwemeAidLastResortHandler.handle(SourceFile:34013426) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.AwemeRecordGetTimeHandler.handle(SourceFile:33882213) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.dislike.FilterLocalDislikeHandler.handle(SourceFile:33816602) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.MonitorDataResponseBeforeHandler.handle(SourceFile:33816599) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.feed.presenter.msghandler.FeedResponseRevertBeforeHandler.handle(SourceFile:34079435) at com.ss.android.ugc.aweme.handler.TetrisChain.LIZ(SourceFile:34013384) at com.ss.android.ugc.aweme.mvp.TetrisBaseListModel.LJJI(SourceFile:33882208) at com.ss.android.ugc.aweme.mvp.TetrisBaseListModel$handleMsg$1.invokeSuspend(SourceFile:17039390) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(SourceFile:17039371) at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(SourceFile:50724991) at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(SourceFile:67305485) at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(SourceFile:100794373) at kotlinx.coroutines.CoroutineStart.invoke(SourceFile:50593832) at kotlinx.coroutines.AbstractCoroutine.start(SourceFile:50331648) at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(SourceFile:67305487) at kotlinx.coroutines.BuildersKt.launch(SourceFile:67174400) at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(SourceFile:100859916) at kotlinx.coroutines.BuildersKt.launch$default(SourceFile:100728832) at com.ss.android.ugc.aweme.mvp.TetrisBaseListModel.handleMsg(SourceFile:16973839) at com.bytedance.common.utility.collection.WeakHandler.com_bytedance_common_utility_collection_WeakHandler_fork_by_async_handleMessage(SourceFile:16908300) at com.bytedance.common.utility.collection.WeakHandler.handleMessage(Unknown Source:3) at X.0WnZ.handleMessage(SourceFile:16973836) at android.os.Handler.dispatchMessage(Handler.java:115) at com.bytedance.qmi.handler.base.QmiHandler.dispatchMessage(SourceFile:17104966) at com.ss.android.ugc.bytex.async.stack.TransmitHandler.dispatchMessage(SourceFile:17039385) at android.os.Looper.loopOnce(Looper.java:298) at android.os.Looper.loop(Looper.java:408) at android.app.ActivityThread.main(ActivityThread.java:9952) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:613) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1074) Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@eb8dce8, 0W0R@b74ef01]
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值