惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
O
OpenAI News
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
Docker
F
Full Disclosure
AI
AI
罗磊的独立博客
博客园 - 【当耐特】
U
Unit 42
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Palo Alto Networks Blog
博客园_首页
H
Help Net Security
量子位
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog RSS Feed
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
S
Secure Thoughts
爱范儿
爱范儿
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
CERT Recently Published Vulnerability Notes
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Securelist

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(二十八)------之Service(转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

        上次介绍了Activity以及Intent的使用,这次就介绍Service,如果把Activity比喻为前台程序,那么Service就是后台程序,Service的整个生命周期都只会在后台执行。Service跟Activity一样也由Intent调用。在工程里想要添加一个Service,先新建继承Service的类,然后到AndroidManifest.xml -> Application ->Application Nodes中的Service标签中添加。

         Service要由Activity通过startService 或者 bindService来启动,Intent负责传递参数。先贴出本文程序运行截图:

本文主要讲解Service的调用,以及其生命周期。

上图是startService之后再stopService的Service状态变化。

上图是bindService之后再unbindService的Service状态变化。

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

 上图是startService之后再Activity.finish()的Service状态变化,Service还在跑着。

上图是bindService之后再Activity.finish()的Service状态变化,Service最后自动unbindService。

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.   
  3. import android.app.Activity;  
  4. import android.app.Service;  
  5. import android.content.ComponentName;  
  6. import android.content.Intent;  
  7. import android.content.ServiceConnection;  
  8. import android.os.Bundle;  
  9. import android.os.IBinder;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. public 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.               
  46.         }  
  47.   
  48.         @Override  
  49.         public void onServiceDisconnected(ComponentName name) {  
  50.               
  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")  
  69.                     testService.this.unbindService(_connection);  
  70.             }  
  71.             else if(v==btnExit)  
  72.             {  
  73.                 testService.this.finish();  
  74.             }  
  75.               
  76.         }  
  77.           
  78.     }  
  79. }  

MyService.java的源码:

  1. package com.testService;  
  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.     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. }