Service入门小例子

本例子参考网上的例子而写,为学习而用

实现功能1.service后台工作,传递参数(数秒)到Activity显示

          2.绑定一个Activity,与之共存亡

环境:android adt    android4.0

主界面:MainStandy.java


package com.example.csdn;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
import android.view.View.OnClickListener;

public class MainStandy extends Activity {

	Button button1;
	Button button2;
	Button button3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_standy);

		button1=(Button)findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(MainStandy.this,CountService.class);
				startService(intent);
				Log.v("MainStandy", "start service!");
			}
		});
		
		button2= (Button)findViewById(R.id.button2);
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(MainStandy.this,CountService.class);
				stopService(intent);
				Log.v("MainStandy", "the service shutdown!");

			}
		});
		
		button3=(Button)findViewById(R.id.button3);
		button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(MainStandy.this,Binder.class);
				startActivity(intent);
				Log.v("MainStandy", "start the binderService!");
				
			}
		});
	}


}


CountService.java

package com.example.csdn;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;



public class CountService extends Service{

	boolean ThreadDisable;
	int count;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public void onCreate(){
		
		super.onCreate();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(!ThreadDisable){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					count++;
					Log.v("CountService", "Count is "+count);
				}
			}
		}).start();
		
	}
	public void onDestroy(){
		super.onDestroy();
		this.ThreadDisable=true;
	}
class ServiceBinder extends Binder{
	
	public CountService getService()
	{
		return CountService.this;
	}
}
}



HuaHua.java

package com.example.csdn;

import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.view.View;

public class HuaHua extends View{

	public HuaHua(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void onDraw(Canvas canvas){
		canvas.drawColor(Color.WHITE);
		Paint textPaint=new Paint();
		textPaint.setColor(Color.RED);
		textPaint.setTextSize(35);
		canvas.drawText("use the BinderService", 10, 20, textPaint);
		textPaint.setColor(Color.BLUE);
		textPaint.setTextSize(18);
		canvas.drawText("if you used the bind service after this Activity down",20,60, textPaint);
		canvas.drawText("the binded service will shutdown with the Activiity", 5, 80, textPaint);
		
	}

}


Binder.java


package com.example.csdn;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class Binder extends Activity{

	CountService countService;
	public void onCreate(Bundle saveInstanceState){
		
		super.onCreate(saveInstanceState);
		setContentView(new HuaHua(this));
		Intent intent=new Intent(Binder.this,CountService.class);
		bindService(intent, conn, Context.BIND_AUTO_CREATE);//bindservice must use the method of the ServiceConnect
	}
	
	
	private ServiceConnection conn=new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			countService=null;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			countService=((CountService.ServiceBinder)service).getService();//use the method of CountService/service 
		}
	};

	protected void onDestroy() {
		super.onDestroy();
		this.unbindService(conn);
		Log.v("MainStandy", "out of BinderService!");
	}
}

记得在布局中添加<Button>,在Mainfast中添加使用到的<service>和<activity>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值