html5中文学习网

您的位置: 首页 > android » 正文

Android中Service(后台服务)详解_Android

[ ] 已经帮助:人解决问题

1.概念: 4tTHTML5中文学习网 - HTML5先行者学习网
(1).Service可以说是一个在后台运行的Activity。它不是一个单独的进程,它只需要应用告诉它要在后台做什么就可以了。 4tTHTML5中文学习网 - HTML5先行者学习网
(2).它要是实现和用户的交互的话需要通过通知栏或者是通过发送广播,UI去接收显示。 4tTHTML5中文学习网 - HTML5先行者学习网
(3).它的应用十分广泛,尤其是在框架层,应用更多的是对系统服务的调用。4tTHTML5中文学习网 - HTML5先行者学习网
2.作用: 4tTHTML5中文学习网 - HTML5先行者学习网
(1).它用于处理一些不干扰用户使用的后台操作。如下载,网络获取。播放音乐,他可以通过INTENT来开启,同时也可以绑定到宿主对象(调用者例如ACTIVITY上)来使用。 4tTHTML5中文学习网 - HTML5先行者学习网
(2).如果说Activity是显示前台页面的信息,那么Service就是在后台进行操作的。如果Service和前台UI进行交互的话可以通过发送广播或者通知栏的方式。4tTHTML5中文学习网 - HTML5先行者学习网
3.生命周期: 4tTHTML5中文学习网 - HTML5先行者学习网
(1).service整体的生命时间是从onCreate()被调用开始,到onDestroy()方法返回为止。和activity一样,service在onCreate()中进行它的初始化工作,在onDestroy()中释放残留的资源。 4tTHTML5中文学习网 - HTML5先行者学习网
(2).**startService()的方式:**onCreate()->onStartCommand()->onStart()->onDestroy() 4tTHTML5中文学习网 - HTML5先行者学习网
(3).**BindService()的方式:**onCreate()->onBinder()->onUnbind()->onDestroy()。onUnbind()方法返回后就结束了。4tTHTML5中文学习网 - HTML5先行者学习网

4.启动方式: 4tTHTML5中文学习网 - HTML5先行者学习网
(1).Service自己不能运行,需要通过某一个Activity或者其它Context对象来调用。 4tTHTML5中文学习网 - HTML5先行者学习网
(2).Service的启动方式有两种: 4tTHTML5中文学习网 - HTML5先行者学习网
Context.startService()和Context.bindService()两种方式启动Service。如果在service的onCreate()方法或者onStart()方法中有耗时的操作,要新开启一个线程。 在需要service的地方通过以上两种方式来启动。 4tTHTML5中文学习网 - HTML5先行者学习网
注意: 4tTHTML5中文学习网 - HTML5先行者学习网
平常使用多的是startService方法,可以把一些耗时的任务放到后台去处理,当处理完成后,可以通过广播或者通知栏来通知前台。4tTHTML5中文学习网 - HTML5先行者学习网

5.以下通过代码来深入理解:4tTHTML5中文学习网 - HTML5先行者学习网

(1).MainActivity.java类:4tTHTML5中文学习网 - HTML5先行者学习网

package com.example.servicetest;import com.example.servicetest.service.MyService;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener { /** 标志位 */ private static String TAG = "com.example.servicetest.MainActivity"; /** 启动服务 */ private Button mBtnStart; /** 绑定服务 */ private Button mBtnBind; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  initView(); } /**  * init the View  */ private void initView() {  mBtnStart = (Button) findViewById(R.id.startservice);  mBtnBind = (Button) findViewById(R.id.bindservice);  mBtnStart.setOnClickListener(this);  mBtnBind.setOnClickListener(this); } @Override public void onClick(View view) {  switch (view.getId()) {  // 启动服务的方式  case R.id.startservice:   startService(new Intent(MyService.ACTION));   break;  // 绑定服务的方式  case R.id.bindservice:   bindService(new Intent(MyService.ACTION), conn, BIND_AUTO_CREATE);   break;  default:   break;  } } ServiceConnection conn = new ServiceConnection() {  public void onServiceConnected(ComponentName name, IBinder service) {   Log.v(TAG, "onServiceConnected");  }  public void onServiceDisconnected(ComponentName name) {   Log.v(TAG, "onServiceDisconnected");  } }; @Override protected void onDestroy() {  super.onDestroy();  System.out.println("-------onDestroy()--");  stopService(new Intent(MyService.ACTION));  unbindService(conn); }}

(2).MyService.java类:4tTHTML5中文学习网 - HTML5先行者学习网

package com.example.servicetest.service;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyService extends Service { /** 标志位 */ private static String TAG = "com.example.servicetest.service.MyService"; /** 行为 */ public static final String ACTION = "com.example.servicetest.service.MyService"; @Override public void onCreate() {  super.onCreate();  System.out.println("----- onCreate() ---"); } @Override public void onStart(Intent intent, int startId) {  super.onStart(intent, startId);  System.out.println("----- onStart() ---"); } @Override public int onStartCommand(Intent intent, int flags, int startId) {  System.out.println("----- onStartCommand() ---");  return super.onStartCommand(intent, flags, startId); } @Override public IBinder onBind(Intent arg0) {  System.out.println("----- onBind() ---");  return null; } @Override public void onRebind(Intent intent) {  System.out.println("----- onRebind() ---");  super.onRebind(intent); } @Override public boolean onUnbind(Intent intent) {  System.out.println("----- onUnbind() ---");  return super.onUnbind(intent); } @Override public void onDestroy() {  System.out.println("----- onDestroy() ---");  super.onDestroy(); }}

4tTHTML5中文学习网 - HTML5先行者学习网
4tTHTML5中文学习网 - HTML5先行者学习网

(3).AndroidManifest.xml4tTHTML5中文学习网 - HTML5先行者学习网

<!-- 注册 -->  <service android:name="com.example.servicetest.service.MyService" >   <intent-filter>    <!-- 用来启动服务的Intent -->    <action android:name="com.example.servicetest.service.MyService" />    <category android:name="android.intent.category.default" />   </intent-filter>  </service>

4tTHTML5中文学习网 - HTML5先行者学习网

4tTHTML5中文学习网 - HTML5先行者学习网

StartService方法: 4tTHTML5中文学习网 - HTML5先行者学习网
(1).当按startService按钮的时候:如下:4tTHTML5中文学习网 - HTML5先行者学习网

4tTHTML5中文学习网 - HTML5先行者学习网

(2).再继续按startService按钮的时候:如下:4tTHTML5中文学习网 - HTML5先行者学习网

4tTHTML5中文学习网 - HTML5先行者学习网

BindService方法:4tTHTML5中文学习网 - HTML5先行者学习网

(1).当按bindService按钮的时候:如下:4tTHTML5中文学习网 - HTML5先行者学习网

4tTHTML5中文学习网 - HTML5先行者学习网

(2).再继续按bindService按钮的时候:如下:4tTHTML5中文学习网 - HTML5先行者学习网

4tTHTML5中文学习网 - HTML5先行者学习网

(3).先按startService按钮再按bindService按钮:如下:4tTHTML5中文学习网 - HTML5先行者学习网

4tTHTML5中文学习网 - HTML5先行者学习网

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助