当前位置:首页 > 第一行代码Android学习笔记完全版
Message是在线程直接传递的消息。可以携带少量信息,what为字符串,arg1/arg2携带整型数据,obj携带Object对象。
(2)Handler Handler用于发送和处理消息。发送一般用sendMessage(message)方法,传递出去的消息,最后到handleMessage()方法中。
(3)MessageQueue 消息队列,用于存放所有通过Handler发送的消息,等待被处理。一个线程只能有一个MessageQueue对象。
(4)Looper 每当MessageQueue中出现一个Message,looper就会将该message取出,传递到Handler的handleMessage()方法处理。一个线程只能有一个Looper对象。
4、AsyncTask
基于异步处理机制,可以方便的进行子线程中的UI操作。 使用AsyncTask需要自定义一个类去继承。
class BackTask extends AsyncTask
Params:如果在执行AsyncTask时需要传递信息给后台,则传入此类参数。如果不需要,则为Void;
Progress:如果在后台执行任务过程中,需要在界面上显示进程,则使用这个参数作为进程的单位,一般为Integer;
Result:后台任务执行完成之后,如果需要对结果进行返回,则使用这个参数作为返回值的类型,比如Boolean。
对于类class BackTask extends AsyncTask
需要重写几个方法: (1)void onPreExecute() 后台任务开始执行前执行,用于进行界面初始化操作,比如显示进度条。
(2)Boolean doInBackground(Void... params)
该方法所有代码都在子线程中进行,在此处理所有耗时任务;
后台任务完成,根据AsyncTask中规定的泛型返回结果,如果规定为Void,则不返回结果;规定为Boolean,则返回true or false;
不能在此进行UI操作,通过调用publishProgress(Progress)方法传递进度。
(3)void onProgressUpdate(Integer... values) 当doInBackgroud()中调用publishProgress()方法时,这个方法就会被调用,在此进行UI操作,更新进度条。
(4)void onPostExecute(Boolean result) 后台任务结束后,会return结果,在此方法中对结果进行相应处理,更新UI,比如提醒任务结束、关闭进度条等。
最后,要启用BackTask,直接写: new BackTask().execute(); 总结:使用AsyncTask诀窍就是,在doInBackground()中处理耗时操作,在onProgressUpdate()中进行UI操作,在onPostExecute()中执行任务收尾工作。
三、Service 1、定义Service
public class MyService extends Service{ public IBinder onBind(Intent intent) { return null; } public void onCreate() { super.onCreate(); Log.i(\ } public int onStartCommand(Intent intent, int flags, int startId) { Log.i(\ return super.onStartCommand(intent, flags, startId);
} public void onDestroy() { Log.i(\ super.onDestroy(); } }
Service类中有一个抽象方法onBind(),还有三个常用方法: onCreate()会在Service创建时调用;
onStartCommand()会在Service启用时调用; onDestory()会在Service销毁时调用。
service还需要在AndroidManifest.xml中注册。
2、启动和停止Service
启动
Intent intent=new Intent(MainActivity.this,MyService.class); startService(intent); 停止
stopService(intent);
onCreate()只在创建时调用一次,而onStartCommand()每次启动都会调用。
3、Activity与Service通信
利用onBind()方法实现两者之间通信,从而可以在Activity中监视和控制Service。 要使得在Activity中操作Service,必须在Service中安插Activity的内线,通过这个内线可以获取Service信息。否则Service就自己行动,与Activity没有联系了。 而这个内线,就是Binder。
(1)Service安插内线Binder
如何在Service中安插Binder呢?在创建MyService时即可安插内线Binder。 (2)onBind()发送内线
安插内线之后,如何将其发送出去呢?
Service很―贴心‖的自己设定了一个发送内线的方法onBind(),通过该方法将binder发送出去。
public class MyService extends Service{
private DownloadBinder mBinder=new DownloadBinder(); class DownloadBinder extends Binder { public void startDownload() { Log.i(\ } public int getProgress() { Log.i(\ return 0; } } public IBinder onBind(Intent intent) { return mBinder; } ………… }
(3)Activity接收内线
在Activity中,通过建立ServiceConnection对象,其中的onServiceConnected()即可接收内线IBinder,通过向下转型,即可成为binder。在此,即可通过binder获取内线传送来的Service的信息了。
private ServiceConnection connection=new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { } public void onServiceConnected(ComponentName name, IBinder service) { binder=(DownloadBinder) service; binder.startDownload(); binder.getProgress(); } };
(4)绑定Activity与Service
Intent intent=new Intent(MainActivity.this,MyService.class); bindService(intent, connection, BIND_AUTO_CREATE); 解除绑定:
if(connection!=null)unbindService(connection);
共分享92篇相关文档