1、介绍:
- Service,服务,是四大组件之一, 和Activity 非常相似, 一般运行在后台, 没有用户界面, 可执行的程序
- Activity 和 Service的区别
不同点:
1、Activity : 可以和用户交互, 页面可见
2、Service : 后台运行, 没有界面
相同点:
service 的用途:播放音乐;后台下载大文件
2、代码:
主类代码:
package com.example.day17;
import android.app.Service;
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;
import android.widget.ImageView;
public class Activity_day17 extends AppCompatActivity {
Intent intent;
MyService_1 myService_1;
Button startbtn;
Button stopbtn;
ImageView img;
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("ComponentName###",name.toString());
myService_1 = ((MyService_1.MyBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day17);
intent = new Intent(this,MyService_1.class);
bindService(intent,connection, Service.BIND_AUTO_CREATE);
startbtn = findViewById(R.id.start);
stopbtn = findViewById(R.id.stop);
img = findViewById(R.id.img);
startbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myService_1.getLoad(img);
}
});
stopbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(connection);
Log.e("unbindService###","已解绑");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
服务代码:
从网络上去获取图片:
package com.example.day17;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MyService_1 extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
/**
* Binder类
*/
class MyBinder extends Binder{
public MyService_1 getService(){
return MyService_1.this;
}
}
public void getLoad(ImageView img){
Log.e("Thread###","开启了线程");
String str = "http://upload.jianshu.io/users/upload_avatars/6416344/5e103cde-c77d-49f9-8a47-2ed4418c564a.jpg";
new MyAsync(str,img).execute();
}
public class MyAsync extends AsyncTask<String,String, Bitmap> {
String urlstr;
ImageView img;
public MyAsync(String urlstr,ImageView img) {
this.urlstr = urlstr;
this.img = img;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
img.setImageBitmap(bitmap);
}
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap = null;
try {
URL url = new URL(urlstr);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
if(connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte b[] = new byte[1024];
int len = 0;
while((len = is.read(b))!= -1){
os.write(b,0,len);
}
is.close();
os.close();
byte[] bs = os.toByteArray();
bitmap = BitmapFactory.decodeByteArray(bs, 0, bs.length);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}
}
效果图:
点击前
点击启动服务之后: