1. 异步处理消息
- Message
1. 用于不同线程之间传递消息,可以携带少量信息 - Handler
1. 用于发送和处理消息
2. sendmessage()方法发送消息
3. handleMessage()方法处理消息 - MessageQueue
1. 用于存储Handler发送的消息
2. 每个线程只有一个MessageQueue对象 - Looper
1. 是一个监控MessageQueue的对象
2. 它会无限循环,当消息队列中有消息就取出,并传递到Handler的handlemessage()方法中
3. 每个线程只有一个Looper对象 - 示意图
2. 使用AsyncTask
- 原理和异步处理消息机制完全相同,但是Android做了封装
- 是一个抽象类,需要子类继承
- 子类继承AsyncTask时,需要指定三个泛型参数
1. Params 在执行AsyncTask时需传入的参数
2. Progress 显示当前进度单位
3. Result 任务完成时,返回值类型 - 定制任务时需要重写的几个方法
1. onPreExecute()- 在任务开始前执行
- 比如显示一个进度条对话框等
2. doInBackground(Params...)
1. 这个方法中所有代码都会在子线程运行
2. 应该在这个方法中处理耗时任务
3. 这个方法不能进行UI操作(更新ui)
3. onProgressUpdate(Progress...)
1. 调用publishProgress(Progress...)方法后,onProgressUpdate(Progress...)方法很快就被调用
2. 这个方法可以对UI更新
4. onPostExecute(Result)
1. 任务执行完毕后并通过return返回的时候调用
2. 比如关闭进度条对话框
3. 服务的基本用法
- service几个常用方法
1. onCreate()- 创建服务时调用
- 当已经创建,不调用,只调用onStartCommand()
2. onStartCommand()
1. 启动服务时调用
3. onDestory()
1. 销毁服务时调用
4. startService() 和stopService() 都是定义在Context类中,在活动中直接调用这两个方法
5. 在service中的任一个位置调用stopSelf()方法,服务停止
4. 服务和活动通信
- 通过Binder对象管理service
- 创建一个ServiceConnection匿名对象
1. 重写onServiceConnected()方法
2. 重写onServiceDisconnected()方法 - 当服务和活动绑定后,可以调用该服务中的Binder提供的方法
- 绑定,先构建Intent,然后调用bindService() 将服务和活动绑定,bindService()方法接受三个参数
1. 第一个,Inetent对象
2. 第二个,创建的ServiceConnection实例,
3. 第三个,标志位 - 接触绑定,调用unbindservice(),参数为ServiceConnection
5. 服务的生命周期
- startService()调用,如果没有创建会先执行onCreate(),然后执行onStartCommand(),如果创建了直接执行onStartCommand()
- 调用stopService() 和 stopSelf() 服务停止
- 调用bindService()获得一个长久连接,回调服务中的onBind()方法
- 回调可以获取onBind()返回的IBinder对象,可以一直通信,直到调用unbindService()方法,断开连接,销毁服务。
- 当一个服务既调用 startService()又调用 bindService(),需要同时调用stopService()和unbindservice()方法才会销毁服务
6. 前台服务
- 需要权限
- Android 8 以后需要添加通道
https://blog.csdn.net/fusu2178192/article/details/125969079
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
String CHANNEL_ID = “channel_id_01”;
String CHANNEL_NAME = “channel_name_test”;
int NOTIFICATION_ID = 1;
NotificationChannel notificationChannel;
Notification notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
} else {
notification = new Notification.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
}
startForeground(1, notification);
}
7. Download demo
- 出现下载超过数据总量
- pause()之后,再次下载
- 原因:断点下载,拼接request head 符号使用错误,导致获取全部数据