Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10039
  • 博文数量: 9
  • 博客积分: 251
  • 博客等级: 二等列兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-19 23:01
文章分类
文章存档

2012年(9)

我的朋友
最近访客

分类: 嵌入式

2012-08-21 15:01:40

2012年8月21日15:04:17
最近在研究一个播放器的源码,但是发现他是配合一个Service实现的功能。关于Service的内容之前没有碰过,所以这篇文章里摘抄的东西较多等自己熟悉了再逐步完善~

Service是Android组件中与Activity最相似的组件,都代表可执行的程序。
Service与Activity的区别在于:
Service一直在后台运行,没有用户界面。它和Activity一样一旦启动具有自己的生命周期。
一 简介
开发Service的步骤,
(1) 定义一个继承自Service的子类;
(2) AndroidManifest.xml文件中配置该Service;

系列生命周期的方法:
abstract IBinder onBind(Intent intent);//返回一个IBinder对象,应用程序可以通过该对象与Service组件通信。
void onCreate() 该Service第一次被创建后立即回调该方法
void onDestroy() 该Service被关闭之前将会回调该方法
void onStartCommand(Intent intent, int flags, int startId); 每次客户端调用startService(Intent)方法启动该Service时都会回调该方法;
boolean onUnbind(Intent intent):当该Service上绑定的所有客户端都断开连接时将会回调该方法;
08-21 22:45
Service的启动要由Activity通过startService或者bindService来启动,Inten负责传递参数。
他们两者的区别就是使Service的周期改变:
               由startService启动的Service必须要有stopService来结束Service,否则会造成Activity结束Service继续运行的情况;
               由bindService启动的Service可以由unbindService来结束,也可以在Activity结束之后(onDestroy)自动结束。

实例:
main.xml
01
02
03    android:orientation="vertical" android:layout_width="fill_parent"
04    android:layout_height="fill_parent">
05    
06        android:layout_height="wrap_content"android:id="@+id/btnStartMyService"
07        android:text="StartMyService">
08    
09        android:layout_height="wrap_content"android:id="@+id/btnStopMyService"
10        android:text="StopMyService">
11    
12        android:layout_height="wrap_content"android:id="@+id/btnBindMyService"
13        android:text="BindMyService">
14    
15        android:layout_height="wrap_content"android:id="@+id/btnUnbindMyService"
16        android:text="UnbindMyService">
17    
18        android:layout_height="wrap_content" android:id="@+id/btnExit"
19        android:text="退出程序">
20
程序源码:
TestService.java
01package com.testService;
02 
03import android.app.Activity;
04import android.app.Service;
05import android.content.ComponentName;
06import android.content.Intent;
07import android.content.ServiceConnection;
08import android.os.Bundle;
09import android.os.IBinder;
10import android.util.Log;
11import android.view.View;
12import android.widget.Button;
13 
14public class TestService extends Activity {
15    Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
16    @Override
17    public void onCreate(Bundle savedInstanceState) {
18        super.onCreate(savedInstanceState);
19        setContentView(R.layout.main);
20        btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
21        btnStartMyService.setOnClickListener(new ClickEvent());
22         
23        btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
24        btnStopMyService.setOnClickListener(new ClickEvent());
25         
26        btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
27        btnBindMyService.setOnClickListener(new ClickEvent());
28         
29        btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
30        btnUnbindMyService.setOnClickListener(new ClickEvent());
31         
32        btnExit=(Button)this.findViewById(R.id.btnExit);
33        btnExit.setOnClickListener(new ClickEvent());
34    }
35    @Override
36    public void onDestroy()
37    {
38        super.onDestroy();
39        Log.e("Activity","onDestroy");
40    }
41     
42    private ServiceConnection _connection = new ServiceConnection() { 
43        @Override
44        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
45            // TODO Auto-generated method stub
46        }
47 
48        @Override
49        public void onServiceDisconnected(ComponentName name) {
50            // TODO Auto-generated method stub
51        } 
52    }; 
53    class ClickEvent implements View.OnClickListener{
54 
55        @Override
56        public void onClick(View v) {
57            Intent intent=new Intent(testService.this,MyService.class);
58            if(v==btnStartMyService){
59                testService.this.startService(intent);
60            }
61            else if(v==btnStopMyService){
62                testService.this.stopService(intent);
63            }
64            else if(v==btnBindMyService){
65                testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);
66            }
67            else if(v==btnUnbindMyService){
68                if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑
69                    testService.this.unbindService(_connection);
70            }
71            else if(v==btnExit)
72            {
73                testService.this.finish();
74            }
75             
76        }
77         
78    }
79}
MyService.java
01package com.testService;
02 
03import android.app.Service;
04import android.content.Intent;
05import android.os.IBinder;
06import android.util.Log;
07 
08public class MyService extends Service {
09    static public String ServiceState="";
10    @Override
11    public IBinder onBind(Intent arg0) {
12        Log.e("Service", "onBind");
13        ServiceState="onBind";
14        return null;
15    }
16    @Override
17    public boolean onUnbind(Intent intent){
18        super.onUnbind(intent);
19        Log.e("Service", "onUnbind");
20        ServiceState="onUnbind";
21        return false;
22         
23    }
24    @Override
25    public void onCreate(){
26        super.onCreate();
27        Log.e("Service", "onCreate");
28        ServiceState="onCreate";
29    }
30    @Override
31    public void onDestroy(){
32        super.onDestroy();
33        Log.e("Service", "onDestroy");
34        ServiceState="onDestroy";
35    }
36    @Override
37    public void onStart(Intent intent,int startid){
38        super.onStart(intent, startid);
39        Log.e("Service", "onStart");
40        ServiceState="onStart";
41    }
42 
43}
例子不太对头~等有空再更新掉重写吧。
阅读(308) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

无赖皮肤2012-08-26 21:58:21

这个代码插入是怎么实现的,比默认的强多了