1.什么是service
是Android的四大组件之一(四大组件都要注册)
长期运行在后台的,不可件没有界面
运行在主线程中,不能做耗时操作
可以跨进程的调用
作用:后台服务
2.service有哪些应用场景
下载文件,播放音乐,购买车票
3.start service方式启动service怎么做(启动和停止及生命周期)
新建类myservice继承service
重写onBind,这里用不到但是系统规定一定要写
重写onStartCommand方法
重写oncreat(),ondestory(),onstart()方法,因为这里要打印生命周期所以要写
下面是MyService.java文件代码:
package com.example.xiaozhen.webapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by xiaozhen on 2018/6/19.
*/
public class Demo1Service extends Service {
private String TAG="Demo1Service";
private int count=0;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e(TAG, "onStart: " );
}
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate: *****************************" );
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
while (count<=100){
count++;
Log.e(TAG, "onStartCommand:-------"+count );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy: " );
super.onDestroy();
}
}
在清单文件中启动service
在activity中用Intent把service和activity联系起来,其中传值 intent.putExtra也是用这个方法
package com.example.xiaozhen.webapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Demo1ServiceActivity extends AppCompatActivity implements View.OnClickListener {
private Button BTN1,BTN2;
private String TAG=”Demo1ServiceActivity”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo1_service);
init();
}
private void init() {
BTN1= (Button) findViewById(R.id.demo1_btn1);
BTN2= (Button) findViewById(R.id.demo1_btn2);
BTN1.setOnClickListener(this);
BTN2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.demo1_btn1:
Intent intent=new Intent(Demo1ServiceActivity.this,Demo1Service.class);
intent.putExtra("name","张三");
startService(intent);
break;
case R.id.demo1_btn2:
Intent intent1=new Intent(Demo1ServiceActivity.this,Demo1Service.class);
stopService(intent1);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy: ****");
}
4.3.bind service方式启动service怎么做(绑定和解绑及生命周期)
why:不能直接调用service里的方法,通过调用自己内部新建的类,得到里面的方法
1.写一个内部类MyBinder 方法继承Binder里面写一个Demo2Service 方法,返回return Demo2Service.this
2.重写onBind,onCreate,onUnbind,ondestory方法
3.然后自定义一个方法在方法里面开启一个线程,这里只是要调用一下这个方法
4.在activity中用通过ServiceConnection调用自己写的类得到里面的方法
下面是service的代码:
package com.example.xiaozhen.webapplication;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by xiaozhen on 2018/6/19.
*/
public class Demo2Service extends Service {
private String TAG="Demo2Service";
private int count=0;
//实例化自己写的binder并且将他赋值给onBind方法
private MyBinder binder = new MyBinder();
//自己写的一个类继承Binder
public class MyBinder extends Binder {
//
public Demo2Service getService(){
return Demo2Service.this;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind: ---------");
//这里返回不能为null,应是上面初始化过的mybinder
return binder;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate: " );
super.onCreate();
}
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG, "onUnbind: " );
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
stopFlag = true;
}
public void dou(){
new Thread(new Runnable() {
@Override
public void run() {
while (count<=100){
count++;
Log.e(TAG, "run: "+count );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
activity
package com.example.xiaozhen.webapplication;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Demo2ServiceActivity extends AppCompatActivity implements View.OnClickListener {
private Button BTN1,BTN2;
private Intent intent;
private String TAG="Demo1ServiceActivity";
private ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Demo2Service.MyBinder myBinder= (Demo2Service.MyBinder) iBinder;
Demo2Service ds=myBinder.getService();
ds.dou();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo2_service);
init();
}
private void init() {
BTN1= (Button) findViewById(R.id.demo2_btn1);
BTN2= (Button) findViewById(R.id.demo2_btn2);
BTN1.setOnClickListener(this);
BTN2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.demo2_btn1:
intent = new Intent(this, Demo2Service.class);
//绑定服务
bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
break;
case R.id.demo2_btn2:
intent = new Intent(this, Demo2Service.class);
//进行解绑
unbindService(serviceConnection);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//一定要解绑
unbindService(serviceConnection);
Log.e(TAG, "onDestroy: ****");
}
}
intent serviice有什么不同
intent service可以队列排序
6.intent service怎么用
继承intent service,在onHandleIntent中写,因为他会自动开启一个子线程所以不用再写一个线程,然后需要注意的是注册service,直接注册会报错,需要在service中写一个无参的构造方法
public Demo3Service(String name) {
super(name);
}
package com.example.xiaozhen.webapplication;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
/**
* Created by xiaozhen on 2018/6/19.
*/
public class Demo3Service extends IntentService {
private String TAG="Demo3Service";
public Demo3Service(){
super("Demo3Service");
}
public Demo3Service(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
int count=0;
while (count<=10){
count++;
Log.e("xx",count+"");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}