初识 Service

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";
	@Override
	public void onCreate() {
		super.onCreate();
		MainActivity.showServiceInfoTextView.append("Service onCreate\r\n");
		Log.v(TAG,"Service--->onCreate");
	}

	@Override
	public void onDestroy() {
		MainActivity.showServiceInfoTextView.append("Service onDestroy\r\n");
		Log.v(TAG,"Service--->onDestroy");
		super.onDestroy();
	}

	@Override
	public 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);
	}

	@Override
	public 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){
			//开始按钮一按下,开始Service
			case 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;
			}
			
		}
		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值