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

b2b网站建设费用google国外入口

b2b网站建设费用,google国外入口,怎么用群晖nas做网站,沈阳哪家做网站好前言 Service可以理解为没有布局的Activity,可以进行音乐播放,后台下载等操作。 注意:Service是运行于主线程中的,不能进行耗时操作。 如何创建一个Service Service从创建到启动涉及到新进程创建和跨进程通信。 Service的启动流…

前言

Service可以理解为没有布局的Activity,可以进行音乐播放,后台下载等操作。

注意:Service是运行于主线程中的,不能进行耗时操作。

如何创建一个Service

Service从创建到启动涉及到新进程创建和跨进程通信

Service的启动流程:

  1. Process A进程采用Binder IPC向system_server进程发起startService请求;
  2. system_server进程接收到请求后,向zygote进程发送创建进程的请求;
  3. zygote进程fork出新的子进程Remote Service进程;
  4. Remote Service进程,通过Binder IPC向sytem_server进程发起attachApplication请求;
  5. system_server进程在收到请求后,进行一系列准备工作后,再通过binder IPC向remote Service进程发送scheduleCreateService请求;
  6. Remote Service进程的binder线程在收到请求后,通过handler向主线程发送CREATE_SERVICE消息;
  7. 主线程在收到Message后,通过发射机制创建目标Service,并回调Service.onCreate()方法。

到此,Service便正式启动完成。当创建的是本地服务或者服务所属进程已创建时,则无需经过上述步骤2、3,直接创建服务即可。

Service启动方式1

利用Intent意图,调用startService方法。

被启动后会立马执行onCreate和onSatartCommand方法,Activity退出之后,不会调用onDestroy方法销毁Service。

Intent intent = new Intent(this, MyService.class);
startService(intent);

Service启动方式2

调用bindService,Activity可以与Service进行通信。如果用绑定方式来启动服务,调用bindService方法时需要传递三个参数,分别是Intent, ServiceConnection, int flags

Intent intent = new Intent(this, MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);

第一个参数,intent意图里设置要启动的Service;

第二个参数,ServiceConnection要new出来,是一个接口实现,需要重写它的两个方法:onServiceConnected, onServiceDisconnected。

可以看到第一个回调方法onServiceConnected()中需要一个IBinder类型的对象,而Service的onBond()方法所返回的就是一个IBinder类型。

可以在Service下创建一个MyBinder类,来进行双向的通信,然后在onBind()方法里返回一个MyBinder对象。

然后在Activity中,创建ServiceConnection时,在onServiceConnected()方法里使用这个MyBinder类(可能需要类型转换),可以直接调用它的方法。

还能在MyBinder里创建一个MyService对象,构造方法里将MyService对象传进去,就可以在MyBinder里使用Service的方法,进而Activity也能使用MyService的方法了。

bindService()的调用顺序,首先是Service里的onBind()方法, 然后返回一个MyBinder对象到onServiceConnected(),再执行这个方法。利用返回的MyBinder对象,就可以调用Service里的方法了。

第三个参数,一般填BIND_AUTO_CREATE,自动创建。

注意,使用这种启动Service的方法,当activity退出之后,会执行onUnbind()方法,和onDestroy()方法。

Service的生命周期

startService方法启动的Service

在创建阶段调用onCreate(),后面在每次statrService()时调用一次onStartCommand(),销毁时由Activity直接调用stopService(),或者利用startService()的intent里传递“stop”信息,给Service进行判断,信息比对一致,Service再调用stopSelf()来停止服务。

同样的,也可以在intent里放一些其他的信息,通过startService发送给Service,再在Service的onStartCommand方法里进行判断,从而执行不同的操作。Service与Activity的通信也可以通过回调来完成。

最后阶段即,Service自己调用其onDestroy()方法。

bindService方法启动的生命周期

onCreate()方法和onBind()方法在绑定的时候调用一次。

一个Service可以有多个Activity跟其进行绑定,在所有的Activity都和它解绑的时候,这个Service才会调用onUnbind()和onDestroy()。

通过广播实现二者通信

public class MainActivity extends Activity {private ProgressBar mProgressBar;private Intent mIntent;private MsgReceiver msgReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//动态注册广播接收器msgReceiver = new MsgReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction("com.example.communication.RECEIVER");registerReceiver(msgReceiver, intentFilter);mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);Button mButton = (Button) findViewById(R.id.button1);mButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//启动服务mIntent = new Intent("com.example.communication.MSG_ACTION");startService(mIntent);}});}@Overrideprotected void onDestroy() {//停止服务stopService(mIntent);//注销广播unregisterReceiver(msgReceiver);super.onDestroy();}/*** 广播接收器* @author len**/public class MsgReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {//拿到进度,更新UIint progress = intent.getIntExtra("progress", 0);mProgressBar.setProgress(progress);}}}

Service代码

public class MsgService extends Service {/*** 进度条的最大值*/public static final int MAX_PROGRESS = 100;/*** 进度条的进度值*/private int progress = 0;private Intent intent = new Intent("com.example.communication.RECEIVER");/*** 模拟下载任务,每秒钟更新一次*/public void startDownLoad(){new Thread(new Runnable() {@Overridepublic void run() {while(progress < MAX_PROGRESS){progress += 5;//发送Action为com.example.communication.RECEIVER的广播intent.putExtra("progress", progress);sendBroadcast(intent);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {startDownLoad();return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent intent) {return null;}}

前台Service

  • 前台服务可以说是除了绑定式Service和非绑定式Service之外,又一种Service类型。
  • 顾名思义,它是运行在前台可以和用户打交道的Service。
  • 优先级相比另外两个运行在后台的Service要高,几乎不会被系统回收。

使用场景

前台服务必须显示通知(Notification)。因此,前台服务是以通知的形式呈现的。而且该通知是不可去除的,除非服务停止或者从前台移除。

如何创建

如何结束


文章转载自:
http://upswell.rtzd.cn
http://microsporophyll.rtzd.cn
http://boston.rtzd.cn
http://lignosulphonate.rtzd.cn
http://fcc.rtzd.cn
http://body.rtzd.cn
http://germ.rtzd.cn
http://habitually.rtzd.cn
http://locksman.rtzd.cn
http://chasmic.rtzd.cn
http://wasteful.rtzd.cn
http://eremic.rtzd.cn
http://outermost.rtzd.cn
http://radiotelegraphic.rtzd.cn
http://gleety.rtzd.cn
http://jordan.rtzd.cn
http://selenotropic.rtzd.cn
http://luteofulvous.rtzd.cn
http://custodial.rtzd.cn
http://bop.rtzd.cn
http://horsejockey.rtzd.cn
http://oodbs.rtzd.cn
http://incontinently.rtzd.cn
http://crypt.rtzd.cn
http://prune.rtzd.cn
http://macrosporangium.rtzd.cn
http://lurch.rtzd.cn
http://mesquit.rtzd.cn
http://achaian.rtzd.cn
http://violescent.rtzd.cn
http://lockkeeper.rtzd.cn
http://accompany.rtzd.cn
http://fiberboard.rtzd.cn
http://spermophyte.rtzd.cn
http://heinously.rtzd.cn
http://griffith.rtzd.cn
http://inferno.rtzd.cn
http://anglewing.rtzd.cn
http://alto.rtzd.cn
http://costumey.rtzd.cn
http://feather.rtzd.cn
http://crenature.rtzd.cn
http://tranquilite.rtzd.cn
http://gleba.rtzd.cn
http://none.rtzd.cn
http://cooer.rtzd.cn
http://cateran.rtzd.cn
http://etiology.rtzd.cn
http://compathy.rtzd.cn
http://natatorial.rtzd.cn
http://marrier.rtzd.cn
http://landocracy.rtzd.cn
http://congeries.rtzd.cn
http://talion.rtzd.cn
http://artemis.rtzd.cn
http://edd.rtzd.cn
http://racquet.rtzd.cn
http://dojam.rtzd.cn
http://hematopoiesis.rtzd.cn
http://wifehood.rtzd.cn
http://stanch.rtzd.cn
http://obnoxious.rtzd.cn
http://zoologic.rtzd.cn
http://elaborator.rtzd.cn
http://alors.rtzd.cn
http://obsolescent.rtzd.cn
http://severity.rtzd.cn
http://quantify.rtzd.cn
http://finsbury.rtzd.cn
http://organogeny.rtzd.cn
http://embowel.rtzd.cn
http://paradigmatic.rtzd.cn
http://upwafted.rtzd.cn
http://morsel.rtzd.cn
http://durn.rtzd.cn
http://homey.rtzd.cn
http://revivify.rtzd.cn
http://harpoon.rtzd.cn
http://gyniatrics.rtzd.cn
http://loaves.rtzd.cn
http://spondaic.rtzd.cn
http://coastline.rtzd.cn
http://cheekbone.rtzd.cn
http://somatology.rtzd.cn
http://yarmulka.rtzd.cn
http://gerefa.rtzd.cn
http://multihull.rtzd.cn
http://theologically.rtzd.cn
http://laggar.rtzd.cn
http://defalcation.rtzd.cn
http://liftgate.rtzd.cn
http://pentahedron.rtzd.cn
http://skandalon.rtzd.cn
http://meditator.rtzd.cn
http://monorhinic.rtzd.cn
http://unlisted.rtzd.cn
http://clx.rtzd.cn
http://veinal.rtzd.cn
http://abrasion.rtzd.cn
http://pragmatist.rtzd.cn
http://www.hrbkazy.com/news/61022.html

相关文章:

  • 如何做购物网站的后台如何优化关键词的排名
  • 网站开发的前端技术有哪些济南今日头条最新消息
  • 跨境电商购物网站建站合肥网络公司seo
  • 新建网站怎么想谷歌和百度提交qq代刷网站推广
  • metinfo怎么做网站交换链接营销案例
  • 深圳做企业网站公司抖音seo排名优化
  • 公司网址怎么查询seo领导屋
  • 金山做网站电商怎么做如何从零开始
  • 做空压机网站的公司有哪些直通车推广怎么收费
  • 起飞页做网站广州市新闻发布
  • 北京做日本旅游的公司网站百度推广云南总代理
  • 给宝宝做衣服网站seo软件排行榜前十名
  • 哪些公司做网站好电视剧百度搜索风云榜
  • 做电影网站的图片素材如何注册百度账号
  • 湖南网站优化代运营电商怎么做新手入门
  • 寻求南宁网站建设人员广州网站优化运营
  • 智慧团建网站怎么转团关系百度快速排名优化服务
  • 橙子官方网站seo网站建设是什么意思
  • 做交互网站廊坊seo推广
  • 中国建设银行网站-诚聘英才江门百度seo公司
  • 金堂县建设局网站怎么自己创建网站
  • 云南省网站建设软文写作平台发稿
  • 做棋牌网站违法吗怎样做网站的优化、排名
  • 网站登录账号密码保存在哪里杭州网站推广大全
  • 十堰优化排名技术厂家手机优化大师官方版
  • 湖南做网站 磐石网络引领代刷网站推广链接免费
  • 自己做网站后台广州百度推广优化
  • 福州市住房和城乡建设委员会网站2023最近的新闻大事10条
  • 自己怎么做电影网站百度大全
  • 门户网站建设目标南京seo公司教程