您的位置:首页 > 移动开发 > Android开发

Android学习日记(yzy):Service的两种启动

2017-07-13 10:27 337 查看
Service的启动方式有两种:①非绑定的启动startService ②绑定的启动bindService

由于在startService方法下启动的生命周期是:onCreate() → onStartCommand() → onDestroy() ,

在bindService方法下启动的生命周期是:onCreate() → onBind() →
onUnbind() → onDestroy() ,所以两种方法我们所写的service不相同。

其次:相关方法和参数为:

void onStartCommand(Intent intent , Int flags, Int startId)

Ibinder onBind(Intent intent)

boolean onUnbind(Intent intent)

startService(Intent intent)

bindService(Intent intent ,ServiceConnection connection , Int flags)

注意项:
无论在调用bindService还是调用startService启动服务,都要注意使用相关的停止服务unbindService和stopService方法,两者启动方法都用则两者停止服务方法都用,方法调用不分先后顺序,其次sdk2.0后方法onStart变为onStartComment,android5.0后,在使用Intent启动服务时必须显性表示。

public class useBindService extends Service {

private final static String TAG = "sService";
private int count;

private MyBinder mBinder = new MyBinder();
public class MyBinder extends Binder{

}

public IBinder onBind (Intent intent){
Log.e(TAG,"onbind()方法回调");
return mBinder;
}

public void onCreate(){
Log.e(TAG,"onCreate()方法回调");
super.onCreate();
}

public boolean onUnbind(Intent intent){
Log.e(TAG,"onUnbind()方法回调");
return true;
}

public void onRebind(Intent intent){

c43b
Log.e(TAG,"onRebind()方法回调");
super.onRebind(intent);
}

public void onDestroy(){
super.onDestroy();
Log.e(TAG,"onDestroy()方法回调");
}
}


public class mService extends Service {

private final static String TAG  = "mService";

public IBinder onBind(Intent intent){
Log.e(TAG,"onbind()方法回调");
return null;
}

public void onCreate(){
super.onCreate();
Log.e(TAG,"onCreate()方法回调");
}

public int onStartCommand(Intent intent,int flages,int startId){
Log.e(TAG,"onSartCommand()方法回调");
return super.onStartCommand(intent,flages,startId);
}

public void onDestroy(){
super.onDestroy();
Log.e(TAG,"onDestroy()方法回调");
}

}


public class MainActivity extends Activity {

private final static String TAG  = "mService";
private final static String IntentAction = "com.yzy.demo.service.M_SERVICE";
private final static String SercondAction ="com.yzy.demo.service.U_SERVICE";
private Intent mIntent ;
private Intent uIntent ;
private ServiceConnection connection ;
private useBindService.MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntent= new Intent();
//初始化非绑定Service的Intent
mIntent.setAction(IntentAction);
//android5.0以后service的intent必须显性表示
mIntent.setPackage("com.demo.yzy.servicedemo");

//初始化绑定Service的Intent
uIntent= new Intent();
uIntent.setAction(SercondAction);
//android5.0以后service的intent必须显性表示
uIntent.setPackage("com.demo.yzy.servicedemo");

connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// myBinder = (useBindService.MyBinder)service;
}

@Override
public void onServiceDisconnected(ComponentName name) {

}
};
}

public void StartClick(View view){
Log.e(TAG,"the service start");
//非绑定Service的启动
//startService(uIntent);

//绑定Service的启动
bindService(uIntent,connection, Service.BIND_AUTO_CREATE);

}

public void StopClick(View view){
Log.e(TAG,"the service stop");
//非绑定Service的关闭
stopService(uIntent);

//绑定Service的关闭
unbindService(connection);
}
}


end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: