Android bindService的例子

package com.example.test2;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.test2.MyService.MyBinder;

/** 
 * @author Rain 
 * date: 2015年3月18日 下午11:10:42 <br/> 
 * Copyright (c) 2015, 珠海中慧微电子 All Rights Reserved.
 */
public class BindServiceTest extends Activity {
	private Button btn_start = null;
	private Button btn_stop = null;
	
	private boolean flag = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_service_test);
		
		btn_start = (Button)findViewById(R.id.btn_start);
		btn_start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(BindServiceTest.this, MyService.class);
				bindService(intent, conn, Context.BIND_AUTO_CREATE);
			}
		});
		btn_stop = (Button)findViewById(R.id.btn_stop);
		btn_stop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				System.out.println("==activity===准备解绑=========");
				//unbindService(conn);//无需调用,因为当前activity结束时,service会自动结束
				BindServiceTest.this.finish();
			}
		});
		flag = false;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("==activity=====onDestroy======");
		super.onDestroy();
		unbindService(conn);//若不在此调用解绑service的方法,会报xxxActivity  has leaked ServiceConnection的异常
	}
	
	private ServiceConnection conn = new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			System.out.println("==activity=====onServiceConnected======");
			MyBinder myBinder = (MyBinder)binder;
			MyService myService = myBinder.getService();
			myService.myMethod();
			flag = true;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			System.out.println("===activity=====onServiceDisconnected======");
		}
		
	};
}

package com.example.test2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

/** 
 * @author Rain 
 * date: 2015年3月18日 下午11:06:55 <br/> 
 * Copyright (c) 2015, 珠海中慧微电子 All Rights Reserved.
 */
public class MyService extends Service {
	private MyBinder myBinder = new MyBinder();
	private boolean isRun = true;

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("====MyService========onBind=======");
		return myBinder;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("====MyService========onUnbind=======");
		return super.onUnbind(intent);
	}
	
	@Override
	public void onDestroy() {
		System.out.println("====MyService========onDestroy=======");
		isRun = false;
		super.onDestroy();
	}

	public class MyBinder extends Binder {
		public MyService getService(){
			return MyService.this;
		}
	}
	
	public void myMethod(){
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				System.out.println("=======线程开始========");
				while(isRun){
					System.out.println("=======线程正在运行========");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println("=======线程结束========");
			}
		}).start();
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button 
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" 启动 "/>
    <Button 
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" 停止 "/>

</LinearLayout>


最后记得在manifest.xml中添加

<service android:name="com.example.test2.MyService" />


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值