当前位置: 首页 > 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.pierceye.com/news/24789/

相关文章:

  • 免费表格模板网站找人做网站一套多少钱
  • 网站顶一下代码跨境电商平台有哪些类型
  • 常用的网站推广建工教育培训机构
  • 免费推广网站都有哪些加盟营销型网站制作
  • 下载php做站的官方网站外贸网络推广网
  • 网站开发 零基础淄博网站电子商城平台建设
  • 心理咨询网站开发网站域名更改后怎么做映射
  • 手机编辑个人简历seo网站诊断方案
  • 工商网站查询企业信息查询官网wordpress邮箱内容怎么修改
  • 那个网站建设好在线做动漫图的网站
  • 自己搭建网站需要什么设计自己的名字图画
  • 北京 网站建设600佛山网站的建设
  • dede网站后台设置wap模板目录wordpress商品分销
  • 镇江有哪些网站网络架构种类
  • 怎样做网站 app教程电商推广联盟
  • 国外财经网站是怎么做的建设工程的在建设部网站
  • 东莞网站建设 餐饮邢台网站建设 冀icp备
  • 化妆品网站设计报告如何快速提高网站权重
  • 云空间布置网站网站建设的工作在哪里找客户资源
  • 广西情最新消息asp网站优化
  • 设计素材网站大全网站手机端网页界面设计
  • 少儿编程课程收费标准windows优化大师免费版
  • 网站建设原型搜索排名竞价
  • Django可以做门户网站吗禹城网站设计
  • 自己怎么设置会员网站梯子国外服务器
  • 石家庄长安区网站建设公司网站域名更改
  • 临邑县住房和城乡建设局网站旅游网站建设与实现
  • 重庆微信网站开发网站开发流程 图书
  • 手机网站创建站点成功网站底部版权代码
  • 眉山网站设计电子商务网站的设计要求