Service服务学习(SimpleRandomServiceDemo)

 

    Android四大组件:(以下是官方文档)

    1.Activitys:

    2.Services:

    3.Broadcast Receivers

    4.Content Providers

     

    Activities

    An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

    An activity is implemented as a subclass of Activity and you can learn more about it in the Activitiesdeveloper guide.

    Services

    A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.

    A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.

    Content providers

    A content provider manages a shared set of application data. You can store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. Through the content provider, other applications can query or even modify the data (if the content provider allows it). For example, the Android system provides a content provider that manages the user's contact information. As such, any application with the proper permissions can query part of the content provider (such as ContactsContract.Data) to read and write information about a particular person.

    Content providers are also useful for reading and writing data that is private to your application and not shared. For example, the Note Pad sample application uses a content provider to save notes.

    A content provider is implemented as a subclass of ContentProviderand must implement a standard set of APIs that enable other applications to perform transactions. For more information, see the Content Providers developer guide.

    Broadcast receivers

    A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user interface, they may create a status bar notificationto alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.

    A broadcast receiver is implemented as a subclass of BroadcastReceiverand each broadcast is delivered as an Intent object. For more information, see the BroadcastReceiver class

     

     

    什么是Service:

    • A service can run in the background to perform work even while the user is in a different application

     

    使用Services的方式:

    1.startService();

    2.bindService();

    官方文档:

    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.

    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

     

     

    当然了,要想使用Service肯定是少不了在全局文件中注册Service,否则没权限

     

    小笔记:

       

      很让我郁闷的是为什么我的ServiceText工程中的R.java为什么不自动更新了,导致我在Activity中定位控件资源时定位不到?     V_V….

       

      还有就是我写的SimpleRandomServiceDemo为什么老是强制关闭呢?

       

       

      Inter Process Communication,IPC(进程间通信,吼吼。),它可以用于解决两个不同Android应用程序进程之间的调用和通信问题。

 

Service生命周期

 

调用startService()启动Service --> onCreate() --> onStart() --> Service正在运行-->Sercice被停止-->onDestroy()--> 关闭Service

 

使用方式有两种

1.启动方式(不能获取Service对象,无法调用Service中的方法)

1.1Context.startService()启动Servide

1.2Context.stopService()终止ServiceService.stopSefl()停止Service

1.31.2中的终止并未真正终止,除非它所在的Activity调用onDestroy()才算真正终止

2.绑定方式(能获取Service对象,可以调用Service中的方法,通过Connection实现)

          2.1Context.bindService()建立服务链接

2.2Context.unbindService()终止链接

最主要的是:同一个Service可以绑定多个服务链接,这样可以同时为多个不同的组件提供服务

 

例:播放MP3暂停

Context.startService();启动服务

Context.bindService();获取Service对象保存播放信息,再用:暂停函数暂停播放

需要注意的是:在此情况下如何调用Context.stopService()终止播放的话并不能停止Service,需要在所有的服务链接关闭后,Service才能真正停止

 

 

 

 

    Intent广播消息时候要有一个唯一标识,此处,Service在全局文件中注册时也要一个唯一标识

    <service android:name=".继承Service的类名">

    代码实现Service后有两种方式启动服务:显式启动和隐式启动

    1.显式启动:

    final Intent serviceIntent = new Intent(this,继承Service的类名.class);

    startService(serviceIntent);

    2.隐式启动:

    final Intent serviceIntent = new Intent();

    serviceIntent.setAction("继承Service的类_完整路径");

     

    <service android:name=".继承Service的类名">

    <intent-filter>

    <action android:name="继承Service的类_完整路径"/>

    </intent-filter>

    </service>

    无论何种方式 :关闭方式都一样的:stopService(serviceIntent);……………….吼吼

你可能感兴趣的:(职场,休闲,结合官方文档学习Service)