网站建站公司,青岛发现51例阳性,我找客户做网站怎么说,室内装饰装修资质证书前言
Service可以理解为没有布局的Activity#xff0c;可以进行音乐播放#xff0c;后台下载等操作。
注意#xff1a;Service是运行于主线程中的#xff0c;不能进行耗时操作。 如何创建一个Service Service从创建到启动涉及到新进程创建和跨进程通信。 Service的启动流…前言
Service可以理解为没有布局的Activity可以进行音乐播放后台下载等操作。
注意Service是运行于主线程中的不能进行耗时操作。 如何创建一个Service Service从创建到启动涉及到新进程创建和跨进程通信。 Service的启动流程
Process A进程采用Binder IPC向system_server进程发起startService请求system_server进程接收到请求后向zygote进程发送创建进程的请求zygote进程fork出新的子进程Remote Service进程Remote Service进程通过Binder IPC向sytem_server进程发起attachApplication请求system_server进程在收到请求后进行一系列准备工作后再通过binder IPC向remote Service进程发送scheduleCreateService请求Remote Service进程的binder线程在收到请求后通过handler向主线程发送CREATE_SERVICE消息主线程在收到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
调用bindServiceActivity可以与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。因此前台服务是以通知的形式呈现的。而且该通知是不可去除的除非服务停止或者从前台移除。 如何创建 如何结束