初识 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;}}}}