Android中的Service概念及用途.docx

上传人:小飞机 文档编号:3060321 上传时间:2023-03-10 格式:DOCX 页数:12 大小:41.16KB
返回 下载 相关 举报
Android中的Service概念及用途.docx_第1页
第1页 / 共12页
Android中的Service概念及用途.docx_第2页
第2页 / 共12页
Android中的Service概念及用途.docx_第3页
第3页 / 共12页
Android中的Service概念及用途.docx_第4页
第4页 / 共12页
Android中的Service概念及用途.docx_第5页
第5页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Android中的Service概念及用途.docx》由会员分享,可在线阅读,更多相关《Android中的Service概念及用途.docx(12页珍藏版)》请在三一办公上搜索。

1、Android中的Service概念及用途Service概念及用途 Android中的Service,其意思是“服务”,它是在后台运行,不可交互的。Service自己不能运行,需要通过某一个Activity或者其它Context对象来调用,如Context .startService 和Context.bindService两种方式启动Service 。 Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果我们退出应用时,Service进程并没有结束,它仍然在后台运行,那我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能

2、想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间的数据是不同的这时候我们可以用Service在后台定时更新,而不用每打开应用的时候在去获取。 如果在Service的onCreate或者onStart方法中做一些很耗时的动作,最好是启动一个新线程来运行这个Service,因为,如果Service运行在主线程中,会影响到程序的UI操作或者阻塞主线程中的其它事情。 Service生命周期 Service的生命周期方法比Activity要少一些,只有onCreate、o

3、nStart、onDestroy。有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。 1)通过startService启动 Service启动的时候会经历生成开始过程,Service停止的时候直接进入销毁过程。而如果是调用者直接退出而没有调用stopService,Service会一直在后台运行。直到下次调用者再启动起来,并明确调用stopService。 2)通过bindService启动 通过bindService方法启动Service,其只会运行onCreate方法,如果调用退出了,Service会调用onUnbind,onDestroyed方法。Servi

4、ce的onCreate方法只会被调用一次。如果先绑定了,那么启动的时候就直接运行Service的onStart方法,如果先启动,那么绑定的时候就直接运行onBind方法。如果先绑定上了,就停止不了,也就是stopService不能用了,只能先unbindService ,再stopService,所以,先启动还是先绑定,是有区别的。 示例1 下面以一个通过Service来播放音乐的例子说明Service的具体用法,其具体界面如下所示:在这个界面中,点击“Start Playing”按钮,即开始打开音乐文件,进行循环播放。点击“Stop Playing”按钮,即关闭音乐,并退出应用程序。其程序结

5、构如下图所示: 可以看到,其功能主要由Music与TestMusicService两个类组成,其源代码如下: package com.shen.service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; pub

6、lic class TestMusicService extends Activity private TextView tv; private Button btn1,btn2; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1 = (Button) this.findViewById(R.

7、id.btn1); btn1.setOnClickListener(new ButtonListener1); btn2 = (Button) this.findViewById(R.id.btn2); btn2.setOnClickListener(new ButtonListener2); private class ButtonListener1 implements OnClickListener public void onClick(View v) TestMusicService.this.startService(new Intent(com.shen.music1); pri

8、vate class ButtonListener2 implements OnClickListener public void onClick(View v) TestMusicService.this.stopService(new Intent(com.shen.music1); finish; package com.shen.service; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; p

9、ublic class Music extends Service private MediaPlayer mp; public IBinder onBind(Intent intent) return null; public void onStart (Intent intent, int startId) super.onStart(intent, startId); if (mp != null) mp.stop; mp = MediaPlayer.create(this,R.raw.music_1); mp.setLooping(true); mp.start; public voi

10、d onDestroy super.onDestroy; mp.stop; mp = null; 另外,需要在Manifest.xml文件中对Service进行注册。其注册信息如下所示: 示例2:Service与Activity通信: Service后端的数据最终还是要呈现在前端Activity之上的,因为启动Service时,系统会重新开启一个新的进程,这就涉及到不同进程间通信的问题了(AIDL)这一节我不作过多描述,当我们想获取启动的Service实例时,我们可以用到bindService和onBindService方法,它们分别执行了Service中IBinder和onUnbind方法。

11、 为了让大家 更容易理解,我写了一个简单的Demo,大家可以模仿,一步一步的来。 第一步:新建一个Android工程,命名为ServiceDemo. 第二步:修改main.xml代码,这里增加了四个按钮,代码如下: 第三步:新建一个Service,命名为MyService.java代码如下: package com.tutor.servicedemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import and

12、roid.text.format.Time; import android.util.Log; public class MyService extends Service /定义个一个Tag标签 private static final String TAG = MyService; /这里定义一个Binder类,用在onBind有方法里,这样Activity那边可以获取到 private MyBinder mBinder = new MyBinder; Override public IBinder onBind(Intent intent) Log.e(TAG, start IBinde

13、r); return mBinder; Override public void onCreate Log.e(TAG, start onCreate); super.onCreate; Override public void onStart(Intent intent, int startId) Log.e(TAG, start onStart); super.onStart(intent, startId); Override public void onDestroy Log.e(TAG, start onDestroy); super.onDestroy; Override publ

14、ic boolean onUnbind(Intent intent) Log.e(TAG, start onUnbind); return super.onUnbind(intent); /这里我写了一个获取当前时间的函数 public String getSystemTime Time t = new Time; t.setToNow; return t.toString; public class MyBinder extends Binder MyService getService return MyService.this; 第四步:修改ServiceDemo.java,代码如下:

15、package com.tutor.servicedemo; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.view.View; import android.vie

16、w.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class ServiceDemo extends Activity implements OnClickListener private MyService mMyService; private TextView mTextView; private Button startServiceButton; private Button stopServiceButton; private Button bin

17、dServiceButton; private Button unbindServiceButton; private Context mContext; /这里需要用到ServiceConnection在Context.bindService和context.unBindService 里用到 private ServiceConnection mServiceConnection = new ServiceConnection /当bindService,让TextView显示MyService里getSystemTime方法的返回值 public void onServiceConnec

18、ted(ComponentName name, IBinder service) mMyService = (MyService.MyBinder)service).getService; mTextView.setText(I am frome Service : + mMyService.getSystemTime); public void onServiceDisconnected(ComponentName name) ; public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState

19、); setContentView(R.layout.main); setupViews; public void setupViews mContext = ServiceDemo.this; mTextView = (TextView)findViewById(R.id.text); startServiceButton = (Button)findViewById(R.id.startservice); stopServiceButton = (Button)findViewById(R.id.stopservice); bindServiceButton = (Button)findV

20、iewById(R.id.bindservice); unbindServiceButton = (Button)findViewById(R.id.unbindservice); startServiceButton.setOnClickListener(this); stopServiceButton.setOnClickListener(this); bindServiceButton.setOnClickListener(this); unbindServiceButton.setOnClickListener(this); public void onClick(View v) /

21、TODO Auto-generated method stub if(v = startServiceButton) Intent i = new Intent; i.setClass(ServiceDemo.this, MyService.class); mContext.startService(i); else if(v = stopServiceButton) Intent i = new Intent; i.setClass(ServiceDemo.this, MyService.class); mContext.stopService(i); else if(v = bindSer

22、viceButton) Intent i = new Intent; i.setClass(ServiceDemo.this, MyService.class); mContext.bindService(i, mServiceConnection, BIND_AUTO_CREATE); else mContext.unbindService(mServiceConnection); 第五步:修改AndroidManifest.xml代码(将我们新建的MyService注册进去如下代码第14行:) 第六步:执行上述工程,效果图如下: 点击startServie按钮时先后执行了Service中o

23、nCreate-onStart这两个方法,打开Logcat视窗效果如下图: 我们这时可以按HOME键进入Settings(设置)-Applications(应用)-Running Services(正在运行的服务)看一下我们新启动了一个服务,效果如下: 点击stopService按钮时,Service则执行了onDestroy方法,效果图如下所示: 这时候我们再次点击startService按钮,然后点击bindService按钮(通常bindService都是bind已经启动的Service),我们看一下Service执行了IBinder方法,以及TextView的值也有所变化了,如下两张图

24、所示: 最后点击unbindService按钮,则Service执行了onUnbind方法,如下图所示: 3. 几个需要注意的地方 1) Service无论以何种方式创建,都是在应用的主线程里创建的,也就是说创建一个Service并不意味着生成了一个新的线程,Service的创建过程是阻塞式的,因此也需要考虑性能,不能影响界面和逻辑上的后续操作。 2) 如果Service自己没有生成新的线程,那它也是运行在应用的主线程里的,因此Service本身并不能提高应用的响应速度和其他的性能,而是说通过这个后台服务生成新的线程来处理比较耗时的操作如大数据的读取等来提高响应,Service自己并不能保证这一点。Service相当于提供了一个这些费时操作的平台,由它在后台创建新线程完成这些任务,以及视各种情况管理这些线程,包括销毁。 3) stopService和unbindService都可以把Service停掉,但是如果希望明确立刻停掉Service,则使用stopService更安全,因为unbindService实质上是将与Service的连接数减一,当减为0的时候才会销毁该服务实例,stopService的效果相当于将连接数立即减为0,从而关闭该服务,所以在选择关闭方式上要视不同情况而定。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号