2.App Components-Services

1.Services

  A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

  A service can essentially take two forms:

    <1>Started

      A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can

        run in the background indefinitely, even if the component (that started it) is destroyed. Usually, a started service performs a single

        operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the

        operation is done, the service should stop itself.

    <2>Bound

      A service is "bound" when an application component binds to it by calling bindService() . A bound service offers a client-server

        interface that allows components to interact with the service, send requests, get results, and even do so across processes with

        interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple

        components can bind to the service at once, but when all of them unbind, the service is destroyed.

  A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process

    (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as

    MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will

    reduce the risk of Application Not Responding (ANR) errors and the application‘s main thread can remain dedicated to user interaction

    with your activities.

2. The Basic

  To create a service, you must create a subclass of Service (or one of its existing subclasses). In your implementation, you need to override

    some callback methods that handle key aspects of the service lifecycle and provide a mechanism for components to bind to the service,

    if appropriate. The most important callback methods you should override are:

    <1>onStartCommand()

      The system calls this method when another component, such as an activity, requests that the service be started, by calling

        startService(). Once this method executes, the service is started and can run in the background indefinitely. If you implement this,

        it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService().

    <2>onBind()

      The system calls this method when another component wants to bind with the service (such as to perform RPC), by calling

        bindService(). In your implementation of this method, you must provide an interface that clients use to communicate with the

        service, by returning an IBinder. You must always implement this method,

    <3>onCreate()

    <4>onDestroy()

  If a component starts the service by calling startService() (which results in a call to onStartCommand()), then the service remains running until

    it stops itself with stopSelf() or another component stops it by calling stopService().

  If a component calls bindService() to create the service (and onStartCommand() is not called), then the service runs only as long as the

    component is bound to it. Once the service is unbound from all clients, the system destroys it.

3. Declaring a service in the manifest

  Like activities (and other components), you must declare all services in your application‘s manifest file.

  To ensure your app is secure, always use an explicit intent when starting or binding your Service and do not declare intent filters for

    the service.

4. Creating a started Service

  When a service is started, it has a lifecycle that‘s independent of the component that started it and the service can run in the background

    indefinitely,

  An application component such as an activity can start the service by calling startService() and passing an Intent that specifies the service

    and includes any data for the service to use. The service receives this Intent in the onStartCommand() method.

  A services runs in the same process as the application in which it is declared and in the main thread of that application, by default. So, if your

    service performs intensive or blocking operations while the user interacts with an activity from the same application, the service will slow

    down activity performance. To avoid impacting application performance, you should start a new thread inside the service.

  Traditionally, there are two classes you can extend to create a started service:

  <1>IntentService

    This is a subclass of Service that uses a worker thread to handle all start requests, one at a time. This is the best option if you don‘t

      require that your service handle multiple requests simultaneously. All you need to do is implement onHandleIntent(), which receives

      the intent for each start request so you can do the background work.

//IntentServiceTest
package com.example.intentservicedemo;

import android.app.IntentService;
import android.content.Intent;

public class IntentServiceTest extends IntentService {

        /**
       * A constructor is required, and must call the super IntentService(String)
       * constructor with a name for the worker thread.
       */
    public IntentServiceTest() {  //be sure to use empty constructor!!!!!
        super("IntentServiceTest");
    }

    /**
       * The IntentService calls this method from the default worker thread with
       * the intent that started the service. When this method returns, IntentService
       * stops the service, as appropriate.
       */
    @Override
    protected void onHandleIntent(Intent intent) {

        // Normally we would do some work here, like download a file.

        String action = intent.getExtras().getString("param");
        if (action.equals("oper1")) {
            System.out.println("Operation1");
        }else if (action.equals("oper2")) {
            System.out.println("Operation2");
        }  

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }  

    }
}
//Mainactivity
package com.example.intentservicedemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      //可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个
        //Operation 1
        Intent startServiceIntent = new Intent("com.test.intentservice");
        Bundle bundle = new Bundle();
        bundle.putString("param", "oper1");
        startServiceIntent.putExtras(bundle);
        startService(startServiceIntent);  

        //Operation 2
        Intent startServiceIntent2 = new Intent("com.test.intentservice");
        Bundle bundle2 = new Bundle();
        bundle2.putString("param", "oper2");
        startServiceIntent2.putExtras(bundle2);
        startService(startServiceIntent2);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intentservicedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".IntentServiceTest">
            <intent-filter >
                <action android:name="com.test.intentservice"/>
            </intent-filter>
        </service>
    </application>

</manifest>

  All this adds up to the fact that all you need to do is implement onHandleIntent() to do the work provided by the client.

  (Though, you also need to provide a small constructor for the service.)

  

  <2>Services

    This is the base class for all services. When you extend this class, it‘s important that you create a new thread in which to do all the

       service‘s work, because the service uses your application‘s main thread, by default, which could slow the performance of any activity

      your application is running.

    As you saw in the previous section, using IntentService makes your implementation of a started service very simple. If, however, you

      require your service to perform multi-threading (instead of processing start requests through a work queue), then you can extend

      the Service class to handle each intent.

//MainActivity
package mirror.android.servicetest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent("servicetest"));
    }
}
//ServiceTest
package mirror.android.servicetest;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ServiceTest extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // We don‘t provide binding, so return null
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onCreate() {
        // Start up the thread running the service.  Note that we create a
        // separate thread because the service normally runs in the process‘s
        // main thread, which we don‘t want to block.  We also make it
        // background priority so CPU-intensive work will not disrupt our UI.
        super.onCreate();

        Log.d("Service_Test","started");
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.d("Service_Test","Sleep ends");
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mirror.android.servicetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".ServiceTest">
            <intent-filter >
                <action android:name="servicetest"/>
            </intent-filter>
        </service>
    </application>

</manifest>

  The appliction will be blocked, and will be warned "The appliction will do too much work ....", because we need to create a new thread,!!!!!!

5. Starting a Service

  You can start a service from an activity or other application component by passing an Intent (specifying the service to start) to startService().

    The Android system calls the service‘s onStartCommand() method and passes it the Intent. (You should never call onStartCommand() directly.)

  The startService() method returns immediately and the Android system calls the service‘s onStartCommand() method. If the service is not

    already running, the system first calls onCreate(), then calls onStartCommand().

  If the service does not also provide binding, the intent delivered with startService() is the only mode of communication between the

    application component and the service. However, if you want the service to send a result back, then the client that starts the service

    can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The

    service can then use the broadcast to deliver a result.

  Multiple requests to start the service result in multiple corresponding calls to the service‘s onStartCommand(). However, only one request to

    stop the service (with stopSelf() or stopService()) is required to stop it.

6. Stopping a service

  the service must stop itself by calling stopSelf() or another component can stop it by calling stopService().

  It‘s important that your application stops its services when it‘s done working, to avoid wasting system resources and consuming battery

    power.

  However, if your service handles multiple requests to onStartCommand() concurrently, then you shouldn‘t stop the service when you‘re done

    processing a start request, because you might have since received a new start request (stopping at the end of the first request would

    terminate the second one). To avoid this problem, you can use stopSelf(int) to ensure that your request to stop the service is always

    based on the most recent start request. That is, when you call stopSelf(int), you pass the ID of the start request (the startId delivered

    to onStartCommand()) to which your stop request corresponds. Then if the service received a new start request before you were able to

    call stopSelf(int), then the ID will not match and the service will not stop.

7. Sending Notifications to the user

  A service can notify user of events using Toast Notifications or Status Bar Notification

8. Managing the lifecycle of a service

  

  

  

时间: 2024-10-19 06:47:41

2.App Components-Services的相关文章

Android Studio中的App Components重要点记述

阅读英文文档而理解的file:///E:/Android2016/sdk/docs/guide/components/fundamentals.html#Components App Compnents 每个component都是系统可以进入你的app的一种方式,但是不是所有的component都是对于user而言的真实的entry points. 共有四种app components: Activity: 每一个activity代表用户界面的一个单独的屏幕,这些activity是相互独立的 S

Android官方文档之App Components(Common Intents)

Intent的真正强大之处在于它的隐式Intent,隐式Intent需要配合Intent-filters使用,有关这方面的概念和知识,您可以参考我翻译的官方文档:<Android官方文档之App Components(Intents and Intent Filters)>. 隐式Intent足够强大,以至于系统提供了大量的Intent方便开发者启动系统应用程序,本文将介绍一些常用的隐式Intent.以及如何自定义intent-filters以匹配隐式intent. 如需阅读官方原文,您可以点

Android官方文档之App Components(Intents and Intent Filters)

Android应用框架鼓励开发者在开发应用时重用组件,本文将阐述如何用组件构建应用程序以及如何用intent将组件联系起来. 如需阅读官方原文,请您点击这个链接: <App Components>. 您还可以参考这些博文: <Using DialogFragments> <Fragments For All> <Multithreading For Performance> 以及这些Training: <Managing the Activity Li

5、二、App Components(应用程序组件):0、概述

二.App Components(应用程序组件) 0.概述 ? App Components Android's application framework lets you create rich and innovative apps using a set of reusable components. This section explains how you can build the components that define the building blocks of your

Android官方文档之App Components(Activities)

Activity是Android四大组件之首,本文将介绍Activity的含义.创建.启动.销毁.生命周期 等. 如需访问官方原文,您可以点击这个链接:<Activities> Activities Activity是一个类,它是Android呈现界面的载体,用于与用户操作交互,如拨号.照相.发送邮件.展示地图 等.每个Activity都承载了一个Window,这个Window用来绘制UI(User Interface).一般情况下,该Window铺满(fill)整个屏幕:有时候,它也可以悬浮

Android官方文档之App Components(Common Intents)(转载)

原文地址:http://blog.csdn.net/vanpersie_9987/article/details/51244558#rd Intent的真正强大之处在于它的隐式Intent,隐式Intent需要配合Intent-filters使用,有关这方面的概念和知识,您可以参考我翻译的官方文档:<Android官方文档之App Components(Intents and Intent Filters)>. 隐式Intent足够强大,以至于系统提供了大量的Intent方便开发者启动系统应用

Android官方文档之App Components(Fragments)

Fragment是Android API中的一个类,它代表Activity中的一部分界面:您可以在一个Activity界面中使用多个Fragment,或者在多个Activity中重用某一个Fragment. 本文将介绍Fragment的定义.创建.添加.移除.生命周期 等,如需访问官方原文,您可以点击这个链接:<Fragments>. Fragments 可以把Fragment想象成Activity中的一个模块,它有自己的生命周期.可以接收输入事件.可以在Activity运行时将Fragmen

Android官方文档之App Components(Loaders)

Loaders在Android 3.0中引入.在Activity和Fragment中,使用Loaders可以方便地加载数据.有关Activity和Fragment的介绍,您可以参考我翻译的官方文档: <Activities> <Fragments> 本文将介绍Loaders API.Loaders的启动.重启.Loaders管理器 等内容,如需访问Loaders的官方原文,您可以点击这个链接:<Loaders>. Loaders Loaders具有如下特性: 它适用于任

Android官方文档之App Components(Tasks and Back Stack)

一个应用中包含了多个Activity实例,每个Activity都有各自的action,每个Activity也可以启动其他Activity,如一个Email应用程序应包含一个显示Email信息列表的Activity.当用户点击列表中的某一项时,显示详细内容的Activity将被启动. 本文将介绍Activity的栈和后退栈(Tasks and Back Stack)的相关知识,您需访问官方原文,您可以点击这个链接:<Tasks and Back Stack>. 一个Activity甚至可以启动其

6、二、App Components(应用程序组件):1、Intents and Intent Filters(意图和意图过滤器)

1.