初识 Service
来源:互联网 发布:linux怎么查看nat 编辑:程序博客网 时间:2024/06/14 23:15
What is a Service?
Most confusion about the Service class actually revolves around what it is not:
- A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
- A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
这是在API文档里介绍的,翻译过来,大概意思就是“Service 不是一个独立的进程,Service也不是一个线程”Service和Activity有些类似,但是更多的是不同。Service没有UI界面,在后台运行,只要启动了就一直运行,不管调用这个Service的Caller是否Running,知道这个Service被onDestroy();
我看来mars老师的视频后,自己也试着完成Service的作业。我创建了一个Android工程“ServiceDemo”
main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/startButton" android:text="Start Service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" /> <Button android:layout_width="wrap_content" android:text="Stop Service" android:layout_height="wrap_content" android:id="@+id/stopButton" android:layout_alignTop="@+id/startButton" android:layout_alignParentRight="true"></Button> <TextView android:id="@+id/showInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/startButton" /> </RelativeLayout>
ServiceDemo.java
package com.jason;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;/** * @author Jason * */public class ServiceDemo extends Service {private final String TAG = "Service";@Overridepublic void onCreate() {super.onCreate();MainActivity.showServiceInfoTextView.append("Service onCreate\r\n");Log.v(TAG,"Service--->onCreate");}@Overridepublic void onDestroy() {MainActivity.showServiceInfoTextView.append("Service onDestroy\r\n");Log.v(TAG,"Service--->onDestroy");super.onDestroy();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.v(TAG,"Service--->onStartCommand:flags"+flags);MainActivity.showServiceInfoTextView.append("Service onStartCommand\r\n");return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent intent) {Log.v(TAG,"Service--->onBind");return null;}}
MainActivity.java
package com.jason;import android.app.Activity;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private Button startButton = null;private Button stopButton = null;public static TextView showServiceInfoTextView = null;public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showServiceInfoTextView = (TextView) findViewById(R.id.showInfo); startButton = (Button) findViewById(R.id.startButton); startButton.setOnClickListener(new ButtonListener()); stopButton = (Button) findViewById(R.id.stopButton); stopButton.setOnClickListener(new ButtonListener()); }class ButtonListener implements OnClickListener{private Intent intent =null;public void onClick(View v) {int id = v.getId();intent = new Intent();intent.setClass(MainActivity.this, ServiceDemo.class);//判断事件源的ID,switch(id){//开始按钮一按下,开始Servicecase R.id.startButton:startService(intent);stopButton.setEnabled(true);startButton.setEnabled(false);//showServiceInfoTextView.setText("Service 启动");break;//停止按钮一按下,Service停止运行。case R.id.stopButton:stopService(intent);stopButton.setEnabled(false);startButton.setEnabled(true);//showServiceInfoTextView.setText("Service 停止");break;}}}}
- 初识 Service
- Service初识
- 初识Service
- 初识 Service(一)
- Windows Service初识
- Web Service初识
- Service初识(一)
- Service初识(二)
- android 初识service(一)
- Android Service(一) Service初识
- web service 初识 axis (一)
- [Android开发入门]初识Service
- 初识android——Service
- XFire初识-使用Eclipse开发Web Service
- [SharePoint 2010] 初识SharePoint BI -- PerformancePoint Service
- [SharePoint 2010] 初识SharePoint BI -- PerformancePoint Service
- [SharePoint 2010] 初识SharePoint BI -- PerformancePoint Service
- web service初识和问题(一)
- IT新技术正在革新中,您是否已经参与了
- C|C++中的静态全局变量,静态局部变量,全局变量,局部变量的区别
- 做一名合格的教师。
- VB 让外部程序在VB程序的窗体里面运行
- a标签的四个伪类选择符
- 初识 Service
- Using Binary Heaps in A* Pathfinding
- Linux中yum使用小结
- 在虚拟机中的小红帽系统上安装VMware tools 方法
- Android实现Service后台下载Notification进度条
- 将Unix Shell实例进行到底
- Shell 基础须知
- Android软件开发之盘点常用系统控件界面大合集(三)
- 实用线程封装(linux)