当前位置: 首页 > news >正文

做服装微商城网站青岛网站建设公司

做服装微商城网站,青岛网站建设公司,免费软件看视频,邢台好蜘蛛网站建设6-1 Servie概述 组件篇——Service 定义:  1.后台运行,不可见,没有界面  2.优先级高于Activity Service是Android系统的后台服务组件#xff0c;适用于开发无界面、长时间运行的应用功能。 Service特点如下#xff1a; 没有用户界面 不会轻易被Android系统终止 在系统…6-1 Servie概述 组件篇——Service 定义:  1.后台运行,不可见,没有界面  2.优先级高于Activity   Service是Android系统的后台服务组件适用于开发无界面、长时间运行的应用功能。 Service特点如下   没有用户界面   不会轻易被Android系统终止   在系统资源恢复后Service也将自动恢复   运行状态   可用于进程间通信 用途:  播放音乐,记录地理信息位置的改变,监听某种动作 注意:  -运行在主线程,不能用它来做耗时的请求或者动作  -可以在服务中开一个线程,在线程中做耗时操作 类型   本地服务(Local Service)     应用程序内部     start方式startService stopService stopSelf stopSelfResult       启动方式使用Service       通过调用Context.startService()启动Service通过调用Context.stopService()或Service.stopSefl()停止Service     Bind方式bindService unbindService       绑定方式使用Service       使用Service的组件通过Context.bindService()建立服务链接通过Context.unbindService()停止服务链接       如果在绑定过程中Service没有启动Context.bindService()会自动启动Service   远程服务(Remote Service)     Android系统内部的应用程序间之间     定义IBinder接口 生命周期   通过startService()方法启动的服务于调用者没有关系,即使调用者关闭了,服务仍然运行想停止服务要调用Context.stopService(),此时系统会调用onDestory(),使用此方法启动时,服务首次启动系统先调用服务的onCreate()--onStart(),如果服务已经启动再次调用只会触发onStart()方法   onCreate()Service的生命周期开始完成Service的初始化工作   onStart()活动生命周期开始   onDestroy()Service的生命周期结束释放Service所有占用的资源   使用bindService()启动的服务与调用者绑定,只要调用者关闭服务就终止,使用此方法启动时,服务首次启动系统先调用服务的onCreate()--onBind(),如果服务已经启动再次调用不会再触发这2个方法,调用者退出时系统会调用服务的onUnbind()--onDestory(),想主动解除绑定可使用Contex.unbindService(),系统依次调用onUnbind()--onDestory();   bindService()绑定Servcie onCreate()和onBindle()将先后被调用。   unbindService()取消绑定ServcieonUnbind()将被调用如果onUnbind()返回true则表示在调用者绑定新服务时onRebind()函数将被调用 非绑定式的service的生命周期  startService()---onCreate()---onStartCommand()---ServingRunning---onStop()---onDestory()服务停止 绑定式的service的生命周期  bindService()---onCreate()---onBind()---用户与服务绑定 在解绑服务 onUnbind()---onDestory()服务停止 start方式特点   -服务跟启动源没有任何联系    -无法得到服务对象 Bind方式特点   -通过Ibinder接口实例返回一个ServerConnnection对象给启动源  -通过ServiceConnection对象的相关方法可以得到Service对象   6-2 Start启动 start方式的启动时   第一次创建Service需要调用onCreate,而后调用onStartCommand()不管调用了多少次的onStartCOmmand()停止的时候只调用一次onDestroy(); StartService1. 使用方法  1写一个MyStartService继承自Service重写它的各种方法onCreate()、onStartCommand()、onDestory()  2在AndroidManifest.xml中注册这个Service  3在主线程Activity中通过startSerice(intent)方式启动  4通过stopService(intent)方式停止 2. 关于StartService  1启动方式是通过启动intent方式实现  2启动之后该Service和启动源没有关系即使主线程退出了service还会继续运行 由于Service和Activity类似属于Android四大组件之一所以我们需要在AndroidManifest.xml中进行注册。我们的组件都需要在AndroidManifest.xml中进行注册。 3.启动Service 显式启动   Intent中指明Service所在的类并调用startService(Intent)函数启动Service final Intent serviceIntent new Intent(this,RandomService.class);startService(serviceIntent); 隐式启动   需要隐式开启Service则可以在注册Service时声明Intent-filter的action属性 service android:name.RandomService intent-filteraction android:nameStartRandomService //intent-filter /service final Intent serviceIntent new Intent(); serviceIntent.setAction(edu.hrbeu.RandomService);   activity_main.xml ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical TextViewandroid:idid/textView1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textStart: /Buttonandroid:idid/startandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:textStartService /Buttonandroid:idid/stopandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:textStopService /TextViewandroid:idid/textView2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textBind /Buttonandroid:idid/bindandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:textBindService /Buttonandroid:idid/playandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:text播放 /Buttonandroid:idid/pauseandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:text暂停 /Buttonandroid:idid/nextandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:text下一首 /Buttonandroid:idid/perviousandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:text上一首 /Buttonandroid:idid/unbindandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:onClickdoClickandroid:textUnBindService //LinearLayout MainActivity.java package com.example.test;import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View;public class MainActivity extends Activity {Intent intent1;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void doClick(View v) {switch (v.getId()) {case R.id.start:intent1 new Intent(MainActivity.this, MyStartService.class);startService(intent1);break;case R.id.stop:stopService(intent1);break;}} } MyStartService.java package com.example.test;import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log;public class MyStartService extends Service {Overridepublic void onCreate() {// TODO Auto-generated method stubLog.i(info, Service--onCreate());super.onCreate();}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubLog.i(info, Service--onStartCommand());return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {// TODO Auto-generated method stubLog.i(info, Service--onDestroy());super.onDestroy();}Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i(info, Service--onBind());return null;}} AndroidManifest.xml ?xml version1.0 encodingutf-8? manifest xmlns:androidhttp://schemas.android.com/apk/res/androidpackagecom.example.testandroid:versionCode1android:versionName1.0 uses-sdkandroid:minSdkVersion15android:targetSdkVersion15 /applicationandroid:allowBackuptrueandroid:icondrawable/ic_launcherandroid:labelstring/app_nameandroid:themestyle/AppTheme activityandroid:name.MainActivityandroid:labelstring/app_name intent-filteraction android:nameandroid.intent.action.MAIN /category android:nameandroid.intent.category.LAUNCHER //intent-filter/activity service android:namecom.example.servicedemo.MyStartService/service/application/manifest   6-3 Bind启动 1.通过绑定服务获取回传数据  通过Ibinder接口实例继承的类里面返回一个Ibinder对象的抽象方法返回一个serviceConnection对象给启动源因为Ibinder里面含有我们要的数据这里可以定义个内部类继承Binder里面做想返回的参数   2.使用onBind()方法的IBinder返回值给启动源返回信息 但是IBinder不能直接使用 需要通过Ibinder接口实例 返回一个ServiceConnection对象给启动源通过ServiceConnection对象的相关方法可以得到ServiceMyBinder是继承自Binder类的而Binder类实际上实现了IBinder接口。 public class MyBinder extends Binder{public MyBindServic getService(){return MyBindService.this;//返回这个Service的实例} } public IBunder onBind(Intent intent){return new MyBinder(); }   3.在启动源定义ServiceConnection 该对象在绑定服务时传输到服务端bindService(intent,conn,Service.BIND_AUTO_CREATE) 绑定后 通过onServiceConnected() 方法获取service对象   4.unbindservice执行后不能重复调用会报错并且在destroy的时候必须解绑 stopserveice则不会   5.服务的两种启动方式   1).通过startService(Intent intent)启动stopService(Intent intent)停止,比较简单。服务启动后与启动源无关也无返回服务本身。需注意要在配置文件中注册服务。   2).通过bindService(Intent intent,ServiceConnection conn,int flags)绑定服务启动unbindService(ServiceConnection conn)去绑定停止,该方式可以返回服务本身与启动源相关。   6.通过bindService绑定服务启动的具体步骤   1Intent intent  new Intent(上下文, 目标服务名.class);    bindService(intent, conn, Service.BIND_AUTO_CREATE);//绑定   2在自定义的服务类中通过自定义一个内部类    public class MyBinder extends Binder {      public MyBindService getService() {        return MyBindService.this;// 获取服务      }    }来返回服务本身    同时在自定义服务类重新父类Service的方法:    public IBinder onBind(Intent intent) {      // TODO Auto-generated method stub      return new MyBinder();    }    该方法可返回服务本身.     3)在启动源的Activity中创建一个ServiceConnection实例,初始化ServiceConnection接口在接口方法中重写方法       ServiceConnection conn  new ServiceConnection() {        //当启动源跟service的连接意外丢失的时候会调用        //比如service崩溃了或被强行杀死了        public void onServiceDisconnected(ComponentName name) {        }        //当启动源跟service成功连接之后会调用这个方法         public void onServiceConnected(ComponentName name, IBinder service) {          myBindService  ((MyBinder)service).getService();//大类转化为自身的小类,获取内部类中的方法从而获得服务本身      }};     4)bindService()中指定ServiceConnection conn参数      bindService(intent2, conn, Service.BIND_AUTO_CREATE);     5)在自定义的继承于Servic类的类中添加需要的方法在启动Service的Activity中可以直接调用服务中的方法。   通过bindService()启动的服务是和启动源Activity绑定在一起的如果Activity退出的时候没有调用unbindService()进行解绑定停止那么程序会报错。所以我们需要在Activity的onDestroy()方法中调用unbindService()进行解绑定。而且对于已经解绑定的服务再次进行解绑定那么也会报错这点和通过startService启动的服务不同stopService()方法可以调用多次。   7.StartService和BindService   1). StartService启动后启动源和Service没有关系BindService调用bindService()启动service后启动源和Service相关在退出activity之前必须要调用unbindService()取消绑定。   2). startService()和bindService()可以混合使用。如果我们想要Activity退出了但是服务还在继续那么我们就要选用startService的方式来启动服务如果我们想要在Activity中获取Service对象那么我们需要用bindService方法结合ServiceConnection来启动Service但是这种方法由于将Service和Activity绑定在了一起所以当Activity退出的时候我们需要unbindService()来停掉Service否则就会报错。   8.BindService   通过bindService()得到的Service是和启动源Activity绑定在一起的在Activity退出的时候需要调用unbindService()进行解绑定停止。   调用bindService()时会调用到目标Service的onBind()函数通过IBinder接口实例返回一个ServiceConnection对象给启动源。然后启动源可以通过ServiceConnection对象得到启动的Service对象 MyBindService.java package com.example.test;import android.app.Service; import android.content.Intent; import android.content.ServiceConnection; import android.os.Binder; import android.os.IBinder; import android.util.Log;public class MyBindService extends Service{Overridepublic void onCreate() {// TODO Auto-generated method stubLog.i(info, BindService--onCreate());super.onCreate();}public class MyBinder extends Binder{public MyBindService getService(){return MyBindService.this;}}Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubLog.i(info, BindService--onBind());return new MyBinder();}Overridepublic void unbindService(ServiceConnection conn) {// TODO Auto-generated method stubLog.i(info, BindService--unbindService());super.unbindService(conn);}Overridepublic void onDestroy() {// TODO Auto-generated method stubLog.i(info, BindService--onDestroy());super.onDestroy();}public void Play(){Log.i(info, 播放);}public void Pause(){Log.i(info, 暂停);}public void Pervious(){Log.i(info, 上一首);}public void next(){Log.i(info, 下一首);} } MainActivity.java package com.example.test;import com.example.test.MyBindService.MyBinder;import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.Menu; import android.view.View;public class MainActivity extends Activity {Intent intent1;Intent intent2;MyBindService service;ServiceConnection conn new ServiceConnection() {Override//当服务跟启动源断开的时候 会自动回调public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}Override//当服务跟启动源连接的时候 会自动回调public void onServiceConnected(ComponentName name, IBinder binder) {// TODO Auto-generated method stubservice ((MyBinder)binder).getService();}};Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void doClick(View v){switch (v.getId()) {case R.id.start:intent1 new Intent(MainActivity.this, MyStartService.class);startService(intent1);break;case R.id.stop:stopService(intent1);break;case R.id.play:service.Play();break;case R.id.pause:service.Pause();break;case R.id.pervious:service.Pervious();break;case R.id.next:service.next();break;case R.id.bind://绑定Service://bindService()方法绑定服务//Intent对象传递给bindService()声明需要启动的Service//Context.BIND_AUTO_CREATE表明只要绑定存在就自动建立Service同时也告知Android系统这个Service的重要程度与调用者相同 intent2 new Intent(MainActivity.this, MyBindService.class);startService(intent2);bindService(intent2, conn, Service.BIND_AUTO_CREATE);//绑定了break;case R.id.unbind:unbindService(conn);break;}}Overrideprotected void onDestroy() {// TODO Auto-generated method stubstopService(intent2);//解绑Service://取消绑定使用unbindService()方法并将ServiceConnnection对象传递给unbindService()方法。//unbindService()方法调用成功后系统并不会再次调用onServiceDisconnected()方法//onServiceDisconnected()方法仅在意外断开绑定时才被调用。 unbindService(conn);//解绑定super.onDestroy();}Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}  转载于:https://www.cnblogs.com/crazyzx/articles/5350862.html
http://www.zqtcl.cn/news/814756/

相关文章:

  • 古典风格网站模版广州网站建设加q.479185700
  • 建站工具推荐网站关键词在哪里添加
  • 国内简约网站汽车最好网站建设
  • 外文网站建设网站项目计划书模板范文
  • 免费婚庆网站模板深圳市龙华区繁华吗
  • 档案馆建设网站邢台又一地被划定高风险区域
  • 网站怎么赚钱的网站asp源码
  • 明星网站怎么设计新手怎么做网络销售
  • ps做网站72分辨率深鑫辉网站建设
  • 购物网站设计的目的html简单登录页面代码
  • 网站导航栏下载网页自助建站
  • 新手建立网站的步骤建设企业网站个人网银
  • 俄罗斯女孩制作论文网站wordpress和hexo
  • 南宁市网站设计wordpress主题安装教程
  • 网站取消备案怎样做国外电子商务网站
  • 学校建设网站费用申请青岛平台公司
  • 平面设计师个人网站怎样登录韵网网站
  • 怎么用eclipse做网站开发推广平台取名字
  • 深圳建网站服务商广东佛山建网站
  • 网站推广公司卓立海创英文网站建设需求
  • 无锡网站营销公司简介最专业网站建设公司首选
  • 中文网站建设小组ios开发者账号申请
  • 月熊志网站福州建网站 做网页
  • 不同的网站有不同的风格宁波设计网站公司
  • 学校网站制作平台电子政务门户网站建设代码
  • 产品推广的网站怎么做网站标题与关键词
  • 青蛙网站建设wordpress修改logo
  • 网站套餐方案引擎搜索对人类记忆的影响
  • 滨州市滨城区建设局网站扎金花网站怎么做
  • 网站开发中视屏怎样编辑到网页上常州建站公司模板