多线程
关键异步处理机制:
android中异步处理机制主要是由四个部分组成,Message,Handler,MessageQueue,Looper组成。
Message:
主要是在线程之间起传递消息的作用,可以使用what,arg1,arg2,(这俩传递整形),还有object传递对象。
Handler:
使用handler.sendmessage传递消息到handler.handleMessage()中,对数据进行处理。
MessageQueue:
每个线程都有一个MessageQueue对象,用于存储Handler发送的messag。
Looper:
messageQueue的管家,调用looper的loop方法从messagequeue中取到handler发送的消息,并传入到handleMessage()方法中
。
异步流程:
主线程创建一个hander对象,重写handleMessage()方法,当在子线程需要进行UI操作时,会创建一个message对象,并通过hander发送到MessageQueue中,Looper调用loop方法无限循环读出MessageQueue中message,并传递给handleMessage(),这个方法是在主线程中,那么他就可以进行UI操作了
代码实例:
package com.example.administrator.mythread;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
public static final int A=0;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0 :
{
textView.setText("change");
}
default:break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
Message message= new Message();
message.what=0;
handler.sendMessage(message);
}
}).start();
}
}
);
}
}
方便的AsyncTask
对于异步处理机制的封装
继承抽象类AsyncTask<传入参数类型,Progress显示后台进度,Result返回结果>
重写其中的方法,通常有以下4个
onPreExecute()
在后台任务执行前开始执行,进行初始化操作,
doInBackgroud(Parms …)
这个方法中的所有代码都会在子线程中运行,任务完成后通过return 语句返回结果,在这个方法中不能进行ui操作,要执行则必须调用pulishProgress(progress…)来完成
on ProgressUpdate(Progress)//这个方法是后台任务还未结束却要修改UI时调用的
在上个方法中调用publishProgress后,会调用此方法,这个方法携带的参数是从后台任务传递过来的,在这个方法中可以进行UI的操作,这里可以理解为回到了主线程。
onPostExecute(Result)
当后台任务执行结束并通过return语句返回时调用,这个方法根据返回的数据,来进行一些UI操作
如何启动AsyncTask?
new 子类().start();
服务
启动和停止服务
Intent intent = new Intent(this,MyService.class)
start/stopService(intent);
服务中的方法:
onCreate()创建服务
onStartCommand():启动服务
onDestroy():销毁服务
public IBinder onBind(Intent intent)
服务和活动的通信
通过Binder对象来实现
服务和活动的绑定,自动创建活动,但不会启动服务
Intent intent = new Intent(this,MyService.class);
bindService(intent,connection,BIND_AUTO_CREATE);
bindService这个方法会在intent中的context之间获取两者间的链接,会回调service中的onBind方法,这个方法返回一个binder对象,这个对象在ServiceConnection对象覆写的方法onServiceConnection中作为参数,来实现在活动中调用服务中的方法,这样就称作服务和活动的通信。
例子
MyService
package com.example.servicetest;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class MyService extends Service {
public MyService() {
}
private DownloadBinder mBinder = new DownloadBinder();
class DownloadBinder extends Binder {
public void startDownload() {
Log.d("MyService", "startDownload executed");
}
public int getProgress() {
Log.d("MyService", "getProgress executed");
return 0;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate executed");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
startForeground(1, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyService", "onDestroy executed");
}
}
-------------------------
MainActivity
package com.example.servicetest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (MyService.DownloadBinder) service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startService = (Button) findViewById(R.id.start_service);
Button stopService = (Button) findViewById(R.id.stop_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
Button bindService = (Button) findViewById(R.id.bind_service);
Button unbindService = (Button) findViewById(R.id.unbind_service);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
Button startIntentService = (Button) findViewById(R.id.start_intent_service);
startIntentService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent); // 启动服务
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent); // 停止服务
break;
case R.id.bind_service:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务
break;
case R.id.unbind_service:
unbindService(connection); // 解绑服务
break;
case R.id.start_intent_service:
// 打印主线程的id
Log.d("MainActivity", "Thread id is " + Thread.currentThread(). getId());
Intent intentService = new Intent(this, MyIntentService.class);
startService(intentService);
break;
default:
break;
}
}
使用前台服务
简单的讲就是创建一个通知,然后不通过Notificationmanager来启动,而是startForegroup(id,nitification)来启用。
前台服务会一直有一个正在运行的图标在系统的通知栏,有些类似通知,但使用前台服务就是为了让服务一直能在前台运行比如像天气预告??
IntentService
因为服务代码在主线程跑容易引起线程阻塞,所以需要用到多线程,但这可能会引起忘记开启和关闭线程的问题,为了避免这个问题使用IntentService
主要是覆写父类的两个方法
onHandleIntent(Intent intent)
这个方法中的代码都在子线程中运行,所以可以把服务中的具体的操作放进去,当运行子线程运行结束后自动的调用ondestroy()方法关闭这个服务。
需要注意的是启动这种服务后,会自行执行onHandleIntent中的代码。