免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2428 | 回复: 2
打印 上一主题 下一主题

Android 的 Service 入门 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-07 17:05 |只看该作者 |倒序浏览
如果把Activity比喻为前台程序,那么Service就是后台程序,Service的整个生命周期都只会在后台执行。Service跟 Activity一样也由Intent调用。在工程里想要添加一个Service,先新建继承Service的类,然后到 AndroidManifest.xml -> Application ->Application Nodes中的Service标签中添加。

Service要由Activity通过startService 或者 bindService来启动,Intent负责传递参数。

startService与bindService都可以启动Service,那么它们之间有什么区别呢?它们两者的区别就是使Service的周期改变。由 startService启动的Service必须要有stopService来结束Service,不调用stopService则会造成 Activity结束了而Service还运行着。bindService启动的Service可以由unbindService来结束,也可以在 Activity结束之后(onDestroy)自动结束。



[代码] main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <Button android:layout_width="wrap_content"
  6.                 android:layout_height="wrap_content" android:id="@+id/btnStartMyService"
  7.                 android:text="StartMyService"></Button>
  8.         <Button android:layout_width="wrap_content"
  9.                 android:layout_height="wrap_content" android:id="@+id/btnStopMyService"
  10.                 android:text="StopMyService"></Button>
  11.         <Button android:layout_width="wrap_content"
  12.                 android:layout_height="wrap_content" android:id="@+id/btnBindMyService"
  13.                 android:text="BindMyService"></Button>
  14.         <Button android:layout_width="wrap_content"
  15.                 android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService"
  16.                 android:text="UnbindMyService"></Button>
  17.         <Button android:layout_width="wrap_content"
  18.                 android:layout_height="wrap_content" android:id="@+id/btnExit"
  19.                 android:text="退出程序"></Button>
  20. </LinearLayout>
复制代码
[代码] TestService.java
  1. package com.testService;

  2. import android.app.Activity;
  3. import android.app.Service;
  4. import android.content.ComponentName;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;

  12. public class TestService extends Activity {
  13.     Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.         btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
  19.         btnStartMyService.setOnClickListener(new ClickEvent());
  20.         
  21.         btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
  22.         btnStopMyService.setOnClickListener(new ClickEvent());
  23.         
  24.         btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
  25.         btnBindMyService.setOnClickListener(new ClickEvent());
  26.         
  27.         btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
  28.         btnUnbindMyService.setOnClickListener(new ClickEvent());
  29.         
  30.         btnExit=(Button)this.findViewById(R.id.btnExit);
  31.         btnExit.setOnClickListener(new ClickEvent());
  32.     }
  33.     @Override
  34.     public void onDestroy()
  35.     {
  36.             super.onDestroy();
  37.             Log.e("Activity","onDestroy");
  38.     }
  39.    
  40.     private ServiceConnection _connection = new ServiceConnection() {  
  41.                 @Override
  42.                 public void onServiceConnected(ComponentName arg0, IBinder arg1) {
  43.                         // TODO Auto-generated method stub
  44.                 }

  45.                 @Override
  46.                 public void onServiceDisconnected(ComponentName name) {
  47.                         // TODO Auto-generated method stub
  48.                 }  
  49.     };  
  50.     class ClickEvent implements View.OnClickListener{

  51.                 @Override
  52.                 public void onClick(View v) {
  53.                         Intent intent=new Intent(testService.this,MyService.class);
  54.                         if(v==btnStartMyService){
  55.                                 testService.this.startService(intent);
  56.                         }
  57.                         else if(v==btnStopMyService){
  58.                                 testService.this.stopService(intent);
  59.                         }
  60.                         else if(v==btnBindMyService){
  61.                                 testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE);
  62.                         }
  63.                         else if(v==btnUnbindMyService){
  64.                                 if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑
  65.                                         testService.this.unbindService(_connection);
  66.                         }
  67.                         else if(v==btnExit)
  68.                         {
  69.                                 testService.this.finish();
  70.                         }
  71.                        
  72.                 }
  73.            
  74.     }
  75. }
复制代码
[代码] MyService.java
  1. package com.testService;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;

  6. public class MyService extends Service {
  7.         static public String ServiceState="";
  8.         @Override
  9.         public IBinder onBind(Intent arg0) {
  10.                 Log.e("Service", "onBind");
  11.                 ServiceState="onBind";
  12.                 return null;
  13.         }
  14.         @Override
  15.         public boolean onUnbind(Intent intent){
  16.                 super.onUnbind(intent);
  17.                 Log.e("Service", "onUnbind");
  18.                 ServiceState="onUnbind";
  19.                 return false;
  20.                
  21.         }
  22.         @Override
  23.         public void onCreate(){
  24.                 super.onCreate();
  25.                 Log.e("Service", "onCreate");
  26.                 ServiceState="onCreate";
  27.         }
  28.         @Override
  29.         public void onDestroy(){
  30.                 super.onDestroy();
  31.                 Log.e("Service", "onDestroy");
  32.                 ServiceState="onDestroy";
  33.         }
  34.         @Override
  35.         public void onStart(Intent intent,int startid){
  36.                 super.onStart(intent, startid);
  37.                 Log.e("Service", "onStart");
  38.                 ServiceState="onStart";
  39.         }

  40. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-08-19 13:37 |只看该作者
学习

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
3 [报告]
发表于 2011-08-21 15:38 |只看该作者
写得不错。学习了啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP