Serivce服务

Service与Activity基本相似的,他们的区别在于Activity主要是在前台运行,而Service是在后台运行,并不提供界面。

开发步骤:

  1. 定义一个继承Service的子类
  2. 在AndroidMainifest.xml文件中进行配置

Service的启动或停止服务有3种方式:

启动 停止 区别
context.startService() context.stopService() 组件退出,服务不退出,服务在系统中
Service.stopSelf() Service.stopSelfResult()  
Context.bindService() context.unbindService() 组件退出,服务也退出

Service的生命周期

void onCreate()//第一次创建时调用
void onStart(Intent intent)//启动
abstract IBinder onBind(Intent intent)//绑定服务到组件,这是service子类必须实现的方法
boolean onUnbind(Intent intent)//解除绑定
void onDestory()//销毁

 创建范例1、

MainActivity.java文件

 1 public class MainActivity extends Activity {
 2     private static final String TAG=".ActivityMain";
 3     //TextView textView=null;
 4      @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         Button startBn=(Button)findViewById(R.id.startBn);
11         Button stopBn=(Button)findViewById(R.id.stopBn);
12         final Intent intent=new Intent(); 
13        intent.setAction("org.crazyit.service.MY_SERVICE");//为intent设置action属性 
14     
15 startBn.setOnClickListener(new Button.OnClickListener(){
16 
17     @Override
18     public void onClick(View arg0) {
19         // TODO Auto-generated method stub
21         startService(intent);
22         
23     }    
24 });
25 stopBn.setOnClickListener(new Button.OnClickListener(){
26 
27     @Override
28     public void onClick(View v) {
29         // TODO Auto-generated method stub
31         stopService(intent);
32         
33     }
34     
35 });
36 
37 }
38 }

MyService.java

 1 package com.example.servicelife;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.IBinder;
 6 import android.util.Log;
 7 
 8 public class Myservice extends Service{
 9     private static final String TAG="Myservice";
10 
11     public void onCreate(){
12         Log.d(TAG,"myService oncreate");
13         super.onCreate();
14     
15    }
16     public int onStartCommand(Intent intent,int flags,int startId){
17         Log.d(TAG,"service is start");
18         return START_STICKY;
19         
20     }
21     public IBinder onBind(Intent intent){
22         
23         return null;
24         
25     }
26     public boolean onUnbind(Intent intent){
27         
28         return super.onUnbind(intent);
29         
30     }
31     public void onDestory(){
32         super.onDestroy();
33     }
34     
35     
36     
37 
38 }

配置文件AndroidMainfest.xml

<service android:name=".Myservice">
              <intent-filter>
                  <action android:name="org.crazyit.service.MYSERVICE" />
                  </intent-filter>
            </service>

改程序总结:

  1. 以上是使用三种方式的第一种
  2. 使用startService()和stopService()来运行服务,service与访问者之间无关联,无法通信和传递数据
  3. 生命周期的方法都是回调的方法

例二、Binder机制

binder机制主要是用于在Activity与service之间进行数据的传递,发送请求等进程间的通。

组件间通信的核心机制是Intent,使用intent来开启activity或service,隐式intent主要是用于实现跨进程的通信,而显式进程主要是用于同一个进程内不同组件之间的通信。

 android中,应用程序与后台服务通常运行在不同的进程之中,各自独立的地址空间,一个进程不能访问里一个的内存空间,如需要访问使用进程通信来完成,最常见使用binder机制。

binder与intent的区别:

原文地址:https://www.cnblogs.com/DASOU/p/3151679.html