赞
踩
这篇文章主要介绍了Android中Service(后台服务)详解,本文讲解了Service的概念、作用、生命周期、启动方式和代码实例等内容,需要的朋友可以参考下
1.概念:
(1).Service可以说是一个在后台运行的Activity。它不是一个单独的进程,它只需要应用告诉它要在后台做什么就可以了。
(2).它要是实现和用户的交互的话需要通过通知栏或者是通过发送广播,UI去接收显示。
(3).它的应用十分广泛,尤其是在框架层,应用更多的是对系统服务的调用。
2.作用:
(1).它用于处理一些不干扰用户使用的后台操作。如下载,网络获取。播放音乐,他可以通过INTENT来开启,同时也可以绑定到宿主对象(调用者例如ACTIVITY上)来使用。
(2).如果说Activity是显示前台页面的信息,那么Service就是在后台进行操作的。如果Service和前台UI进行交互的话可以通过发送广播或者通知栏的方式。
3.生命周期:
(1).service整体的生命时间是从onCreate()被调用开始,到onDestroy()方法返回为止。和activity一样,service在onCreate()中进行它的初始化工作,在onDestroy()中释放残留的资源。
(2).**startService()的方式:**onCreate()->onStartCommand()->onStart()->onDestroy()
(3).**BindService()的方式:**onCreate()->onBinder()->onUnbind()->onDestroy()。onUnbind()方法返回后就结束了。
4.启动方式:
(1).Service自己不能运行,需要通过某一个Activity或者其它Context对象来调用。
(2).Service的启动方式有两种:
Context.startService()和Context.bindService()两种方式启动Service。如果在service的onCreate()方法或者onStart()方法中有耗时的操作,要新开启一个线程。 在需要service的地方通过以上两种方式来启动。
注意:
平常使用多的是startService方法,可以把一些耗时的任务放到后台去处理,当处理完成后,可以通过广播或者通知栏来通知前台。
5.以下通过代码来深入理解:
(1).MainActivity.java类:
- 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类:
- 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();
- }
- }
(3).AndroidManifest.xml
- <!-- 注册 -->
- <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>
StartService方法:
(1).当按startService按钮的时候:如下:
(2).再继续按startService按钮的时候:如下:
BindService方法:
(1).当按bindService按钮的时候:如下:
(2).再继续按bindService按钮的时候:如下:
(3).先按startService按钮再按bindService按钮:如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。