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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的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. }