码迷,mamicode.com
首页 > 移动开发 > 详细

Android四大组件之Service

时间:2015-11-03 22:58:48      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

服务(Service) 是一种在后台运行,没有界面的组件,由其他组件调用开始。Android 中的服务和 Windows 中的服务是类似的东西,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序。

服务(Service)的实现

1. 定义一个类EchoService并继承自Service,重写 onCreate()、 onStart(Intent intent, int startId)、 onBind(Intent intent)、 onUnbind(Intent intent)、onDestroy() 中需要的方法。

 1 package hx.cn.usingservice;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.IBinder;
 6 
 7 /**
 8  * Created by YQ on 2015/11/3.
 9  */
10 public class EchoService extends Service {
11     @Override
12     public IBinder onBind(Intent intent) {
13         return null;
14     }
15 
16     @Override
17     public void onCreate() {
18         super.onCreate();
19         System.out.println("oncreat()");
20     }
21 
22     @Override
23     public void onDestroy() {
24         super.onDestroy();
25         System.out.println("ondestory()");
26     }
27 }

 

2. 在清单文件的 <application> 节点下声明 <service>。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="hx.cn.usingservice" >
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:theme="@style/AppTheme" >
10         <activity
11             android:name=".MainActivity"
12             android:label="@string/app_name" >
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15 
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19         <service android:name=".EchoService"></service>
20     </application>
21 
22 </manifest>

注:服务不能自己运行,需要通过调 用 Context.startService() 或 Context.bindService() 方法启动服务。这两个方法都可以启动 Service,但是它们的使用场合有所不同。使用 startService() 方法启用服务,访问者与服务之间没有关联,即使访问者退出了,服务仍然运行。使用 bindService() 方法启用服务,访问者与服务绑定在了一起,访问者一旦退出,服务也就终止。采用 Context.startService() 方法启动服务,只能调用 Context.stopService() 方法结束服务,服务结束时会调用 onDestroy() 方法。

3.以下是MainActivity中实现的主要代码:

 1 private Button btn_StartService,btn_StopService;
 2     private Intent IntentService;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7 
 8         IntentService=new Intent(this,EchoService.class);
 9 
10         btn_StartService=(Button)findViewById(R.id.btn_StartService);
11         btn_StopService=(Button)findViewById(R.id.btn_StopService);
12 
13         btn_StartService.setOnClickListener(this);
14         btn_StopService.setOnClickListener(this);
15     }
16 
17 
18 
19 
20 
21 
22 
23 @Override
24     public void onClick(View view) {
25         switch (view.getId()){
26             case R.id.btn_StartService:
27                 startService(IntentService);
28                 break;
29             case R.id.btn_StopService:
30                 stopService(IntentService);
31                 break;
32         }

 

Android四大组件之Service

标签:

原文地址:http://www.cnblogs.com/yunqing/p/4934556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!