基于AndroidService生命周期的详细介绍
Service概念及用途:
Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行,那我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以用Service在后台定时更新,而不用每打开应用的时候在去获取。
Service生命周期 :
AndroidService的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。
Service与Activity通信:
Service后端的数据最终还是要呈现在前端Activity之上的,因为启动Service时,系统会重新开启一个新的进程,这就涉及到不同进程间通信的问题了(AIDL),当我们想获取启动的Service实例时,我们可以用到
1、添加一个类,在MainActivity所在包之下
publicclassLServiceextendsService{
privatestaticfinalStringTAG="LService";
@Override
publicIBinderonBind(Intentintent){
Log.i(TAG,"onbind");
returnnull;
}
@Override
publicvoidonCreate(){
Log.i(TAG,"oncreate");
super.onCreate();
}
@Override
publicvoidonStart(Intentintent,intstartId){
Log.i(TAG,"onstart");
super.onStart(intent,startId);
}
@Override
publicvoidonDestroy(){
Log.i(TAG,"ondestoty");
super.onDestroy();
}
@Override
publicbooleanonUnbind(Intentintent){
Log.i(TAG,"onubind");
returnsuper.onUnbind(intent);
}
publicStringgetSystemTime(){
Timet=newTime();
t.setToNow();
returnt.toString();
}
publicclassLBinderextendsBinder{
LServicegetService(){
returnLService.this;
}
}
}
2、在程序界面文件中添加控件
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="wecclometoLivingstone"sbolg"/>
<Button
android:id="@+id/startservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startService"/>
<Button
android:id="@+id/stopservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stopService"/>
<Button
android:id="@+id/bindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="bindService"/>
<Button
android:id="@+id/unbindservice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="unbindService"/>
3、修改MainActivity中的方法,以及让MainActivity类实现OnClickListener接口
publicclassMainActivityextendsActivityimplementsOnClickListener{
privateLServicemLService;
privateTextViewmTextView;
privateButtonstartServiceButton;
privateButtonstopServiceButton;
privateButtonbindServiceButton;
privateButtonunbindServiceButton;
privateContextmContext;
//这里需要用到ServiceConnection,在Context.bindService和context.unBindService()里用到
privateServiceConnectionmServiceConnection=newServiceConnection(){
//当bindService时,让TextView显示LService里getSystemTime()方法的返回值
@Override
publicvoidonServiceConnected(ComponentNamename,IBinderservice){
mLService=((LService.LBinder)service).getService();
mTextView.setText("IamfromService:"+mLService.getSystemTime());
}
publicvoidonServiceDisconnected(ComponentNamename){
}
};
publicvoidsetupViews(){
mContext=MainActivity.this;
mTextView=(TextView)findViewById(R.id.text);
startServiceButton=(Button)findViewById(R.id.startservice);
stopServiceButton=(Button)findViewById(R.id.stopservice);
bindServiceButton=(Button)findViewById(R.id.bindservice);
unbindServiceButton=(Button)findViewById(R.id.unbindservice);startServiceButton.setOnClickListener(this);
stopServiceButton.setOnClickListener(this);
bindServiceButton.setOnClickListener(this);
unbindServiceButton.setOnClickListener(this);
}
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
@Override
publicvoidonClick(Viewv){
if(v==startServiceButton){
Intenti=newIntent(MainActivity.this,LService.class);
mContext.startService(i);
}elseif(v==stopServiceButton){
Intenti=newIntent(MainActivity.this,LService.class);
mContext.stopService(i);
}elseif(v==bindServiceButton){
Intenti=newIntent(MainActivity.this,LService.class);
mContext.bindService(i,mServiceConnection,BIND_AUTO_CREATE);
}else{
mContext.unbindService(mServiceConnection);
}
}
}
4、注册Service
<service
android:name=".LService"
android:exported="true">
</service>
5、运行程序
点击startService
点击stopService
点击bindService
点击unbindService
先点击startService,再依次点击bindService和unbindService
相关文章
- 基于深度学习的路面坑洞检测(详细教程)
- 基于LM331的电压-频率转换电路详细介绍[通俗易懂]
- 渗透测试|HackTheBox–Traverxec靶机详细渗透过程
- 基于BSC链智能合约系统开发详细方案(逻辑理念)
- 深入解析HTTP(web开发详细理解原理)【JavaWeb】
- 详解 MongoDB数据库的安装详细步骤
- Linux-命令-awk详细详解程序员
- Oracle 视图 ALL_REWRITE_EQUIVALENCES 官方解释,作用,如何使用详细说明
- Oracle 视图 USER_ANALYTIC_VIEW_MEAS_CLASS 官方解释,作用,如何使用详细说明
- Oracle 视图 V$OSSTAT 官方解释,作用,如何使用详细说明
- Oracle 视图 V$SQL_PLAN 官方解释,作用,如何使用详细说明
- Debug和Release的区别是什么(非常详细)?
- MySQL中bool类型的详细解释和使用方法(mysql中bool类型)
- 深入浅出 Redis 最详细最易懂的 Redis 教程(深入redis pdf)
- 比较详细的基于CentOS的WWW服务器架设指南
- asp.netWeb.config详细配置说明
- androidlistview优化几种写法详细介绍
- 基于B-树和B+树的使用:数据搜索和数据库索引的详细介绍
- 基于php伪静态的实现详细介绍
- DataSet.Tables[].Rows[][]的用法详细解析