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启动服务时必须显性表示。
end
由于在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
相关文章推荐
- Android Service两种启动方式及 生命周期
- android基础学习之service的启动和关闭细解
- Android Service 两种启动方法和区别
- Android学习之Service开机启动
- Android学习日记(yzy):显示application并调用
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- Intent启动系统组件(activity,service,BroadReceiver)-android学习之旅(四十九)
- 【Android学习日记】(八) Service的简单使用例子
- Android开发学习笔记:Service的简介和启动方式
- Android Service的两种启动方式
- Android中,关于service的启动两种方式描述不正确的是
- Android -- service两种启动方式startService与bindService
- Android组件之Service学习(1)两种使用方式与生命周期
- Android开发学习笔记:Service的简介和启动方式
- Android学习日记(yzy):opengl 绘制2d图形 基本框架
- android service的两种启动方式
- android面试常用 service的两种启动方式
- android 启动service的两种方式有什么不同
- Android Service的两种启动方式
- Android学习日记(yzy):Broadcast Receiver的注册和发送