两种服务(StartService|两种服务(StartService 和bindService)

概念:
1、startService(Intent)通过这种方式开启的服务,执行的生命周期方法:
第一次调用startService的时候:onCreate→onStartCommand
再次调用startService的时候:只执行onStartCommand
2、想停止用startService开启的服务要使用stopService(Intent),stopService执行之后,Service会走onDestroy()方法,执行之后Service销毁,再次调用stopService没有反应
3、如果在Activity中通过startService方法开启一个服务,当Activity退出的时候Service不会销毁,依然在后台运行,只有手动调用stopService或者在应用管理器中关闭Service,服务才会销毁
4、通过startService可以提高应用的优先级
在MainActivity中代码*************
···
public class MainActivity extends Activity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void start(View v){ //使用显示意图开启service服务 Intent service = new Intent(); //通过方法开启服务 startService(service); } public void stop(View v){ Intent intent = new Intent(); }

【两种服务(StartService|两种服务(StartService 和bindService)】}
···
在创建一个类 MyService 继承Sercive
-----------------代码如下-----------
···
public class MyService extends Service{
@Override public IBinder onBind(Intent intent) { Log.e("TAG", "onBind"); return null; } @Override public void onCreate() { Log.e("TAG", "onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("TAG", "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e("TAG", "onDestroy"); super.onDestroy(); }

}
···
xml.---------------
···
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


···
BindService--------------------------
1、开启服务时生命周期比较:
bindService onCreate→onBind(只会执行一次)
startService onCreate→onStartCommand(调用一次startService执行一次)
2、startService开启的服务跟Activity没有关系,bindService开启的服务,跟Activity之间不求同生,但求同死,Activity退出的时候必须通过unbindService关闭服务
3、startService结束的时候stopService可以调用多次,只有第一次调用的时候有效,bindService结束的时候unbindService只能调用一次,调用多次应用会抛异常
4、bindService的时候传入的第二个参数是ServiceConnection,只有当onBind方法返回不为空的时候才会调用onServiceConnected
布局中*****************
···
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bind开启服务"
android:onClick="start"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bind关闭服务"
android:onClick="stop"
/>

···
在MainActivity中实现这三个方法
我们写一个类继承Service
···
package com.example.test23_bindsecevice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class BindService extends Service{
@Override public IBinder onBind(Intent intent) { Log.e("TAG", "onBind"); return new MyBinder(); }

@Override
public void onCreate() {
Log.e("TAG", "onBind");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e("TAG", "onDestroy");
super.onDestroy();
}
public void showToast(String s){
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
public class MyBinder extends Binder{
public void method(String s){
showToast(s);
}
public void showToast2(String s){
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
}
···
在MainActivity中***********
···
public class MainActivity extends Activity {
private MyConnection conn;
MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
//把service关闭 解除跟当前Activiy的绑定
unbindService(conn);
} public void start(View v){ Intent service = new Intent(this,BindService.class); //通过绑定的方式开启service conn = new MyConnection(); //使用bindService 开启反服务 //参数 1. intent 包含要启动的service //2.ServiceConnection接口 通过它可以接受服务开启或者停止的消息 //3.开启服务时操作的选项 一般传入BIND_AUTO_CREATE自动创建service bindService(service, conn, BIND_AUTO_CREATE); //绑定存在自动创建} public void stop(View v){ //使用bindService开启服务要是用unbindService停止 unbindService(conn); } public void method(View v){

// BindService service = new BindService();
// service.showToast("12112");
myBinder.method("dfdg");
myBinder.showToast2("4512") ;
} private class MyConnection implements ServiceConnection{@Override//当服务与Activity 连接时建立 public void onServiceConnected(ComponentName name, IBinder service) { //只有当service onbind方法返回值不为null 调用 Log.e("TAG", "onServiceConnected"); myBinder=(MyBinder) service; }@Override public void onServiceDisconnected(ComponentName name) { Log.e("TAG", "onServiceDisconnected"); }}

}
···
混合服务:
混合开启,混合关闭,
*************xml********
···
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start开启"
android:onClick="start"
/>

···
;写一个类继承Service
************代码如下***************
···
public class MixStartService extends Service{
@Override public IBinder onBind(Intent intent) { Log.e("TAG", "onBind"); return new Binder(); }

@Override
public void onCreate() {
Log.e("TAG", "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("TAG", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e("TAG", "onDestroy");
super.onDestroy();
}
}
···
在MainActivity中****************
···
public class MainActivity extends Activity {
MyConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//start方式
public void start(View v){
Intent service = new Intent(this,MixStartService.class);
startService(service);
}public voidstop(View v){ Intent service = new Intent(this,MixStartService.class); stopService(service); } //绑定方式

public void startbind(View v){
Intent service = new Intent(this,MixStartService.class);
conn=new MyConnection();
bindService(service, conn, BIND_AUTO_CREATE); } public voidunbind(View v){ unbindService(conn); } private class MyConnection implements ServiceConnection{@Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e("TAG", "onServiceConnected"); }@Override public void onServiceDisconnected(ComponentName name) {}}

}
···

    推荐阅读