最基本的应用那就开启和关闭了 我们这里写一个demo
并讲解一下两个开启(StartService,BindService)有什么区别
package com.zy.servicesdemo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Intent intent;
MyServiceConnention myServiceConnention=new MyServiceConnention();
MyService.SericesBinder sericesBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, MyService.class);
}
public void onclick(View v) {
switch (v.getId()) {
case R.id.btn_start:
startService(intent);
break;
case R.id.btn_stop:
stopService(intent);
break;
case R.id.btn_start_bind:
bindService(intent, myServiceConnention, Context.BIND_AUTO_CREATE);
break;
case R.id.btn_stop_bind:
unbindService(myServiceConnention);
break;
case R.id.btn_format:
sericesBinder.changeFormatBinder("yyyy-MM-dd ss");
break;
default:
break;
}
}
private class MyServiceConnention implements ServiceConnection {
/*服务绑定成功执行*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
sericesBinder= (MyService.SericesBinder) service;
}
/*服务销毁*/
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="开始"
/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="结束"
/>
<Button
android:id="@+id/btn_start_bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="绑定"
/>
<Button
android:id="@+id/btn_stop_bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="解绑"
/>
<Button
android:id="@+id/btn_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="改变输出时间格式"
/>
</LinearLayout>
服务
package com.zy.servicesdemo;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyService extends Service {
MyThread myThread;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
myThread.start();
return new SericesBinder();
}
@Override
public void onCreate() {
super.onCreate();
myThread = new MyThread();
Log.e("services start", "服务创建");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("services stop", "服务销毁");
myThread.setStop();
}
/*服务执行过程*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
myThread.start();
return super.onStartCommand(intent, flags, startId);
}
public void changeFormat(String format) {
if (myThread != null)
myThread.setFormet(format);
}
public class MyThread extends Thread {
boolean stop = true;
public void setStop() {
stop = false;
}
String formet = "yyyy-MM-dd hh:mm:ss";
public void setFormet(String formet) {
this.formet = formet;
}
@Override
public void run() {
while (stop) {
SimpleDateFormat sdf = new SimpleDateFormat(formet);
String format = sdf.format(new Date());
Log.e("services running", sdf.format(new Date()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class SericesBinder extends Binder {
void changeFormatBinder(String format) {
changeFormat(format);
}
}
}
startService 开启的程序 后台将持续存在 如果这个进程不死 app 服务也不会死 即使页面关闭了 也不会影响
bindService 开启的service生命周期和他绑定的页面的生命周期相辅相成 如果页面关闭他也会随之关闭
更详细的区别
1. startService和bindService关系?
服务不能自己运行。一旦Activity中调用了startService()方法启动Service后,Activity就不能直接控制Service了。这时就需要bindService()把Activity和Service联系起来,之后就能在Activity中指挥Service去工作了。
startService()和bindService()都能启动Service,它们的调用顺序也会对Service产生影响,具体影响见下文。
2. startService ()时Service的生命周期
通过startService(),Service会经历 onCreate() –> onStart() 启动Service。然后stopService()的时候直接onDestroy()。如果调用者直接退出而没有调用stopService(),那么Service会一直在后台运行。 注意在Service的一个生命周期之内只会调用一次onCreate()方法,stopService()之前若多次startService()则只会调用onStart()方法。
3. bindService()时Service的生命周期
如果打算采用bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onBind()方法。这个时候调用者和服务绑定在一起,调用者unbindService()退出了,系统就会先调用服务的onUnbind()方法,接着调用onDestroy()方法。多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被多次调用)。
如果bindService()之前Service已经在运行了,那么这是调用unbindService()只会onUnbind()而不会onDestory()。