开启服务的两种方式比较以及理解

start方式开启服务的特点

      [1]定义四大组件的方式一样

      [2]定义一个类继承Service

      [3]第一次点击按钮开启服务  服务执行onCreat方法和onStart方法

      [4]第二次点击按钮  再次开启服务  服务执行onStart方法

      [5]服务一旦被开启   服务就会后台长期运行  直到用户手工停止

bindService方式开启服务

   [1]第一次点击按钮  会执行服务的onCreate和onBind方法

   [2]当onBind方法返回null的时候  onServiceConnected方法不执行

   [3]第二次点击按钮  服务器没有响应

   [4]调用者(activity)和服务之间 同时销毁

   [5]服务不可以多次解绑  多次解绑会报异常

   [6]bind方式开启服务  服务不能再设置页面找到服务  相当于隐形的服务

为什么要引入bindService

     目的是为了调用服务里的方法

通过bindService方式调用服务方法的过程

[1]在服务的内部定义一个方法  让activity 去调用

public void banZheng(int money) {
		if (money>=1000) {
			Toast.makeText(getApplicationContext(), "钱到位明天来取证!!!", 1).show();
		}else {
			Toast.makeText(getApplicationContext(), "这点钱还想办事。。。。。。。", 1).show();
		}
	}

[2]在服务的内部定义一个中间人对象(IBinger)

//定义中间人(IBinder)
	public class MyBinder extends Binder{
		public void callBanZheng(int money) {
			//调用办证的方法
			banZheng(money);
		}
	}

[3]把定义的中间人对象在onBind方法中返回

@Override
	public IBinder onBind(Intent intent) {
		//把定义的中间人对象返回
		return new MyBinder();
	}

[4]在mainActivity的onCreate方法中调用bindservice  目的是为了获取我们定义的中间人对象

conn = new Myconn();
		Intent intent = new Intent(this,BanZhengService.class);
		bindService(intent, conn, BIND_AUTO_CREATE);

[5]获取中间人对象

@Override  //链接成功调用
		public void onServiceConnected(ComponentName name, IBinder service) {
			myBinder = (MyBinder) service;//获取中间人对象
		}

[6]拿到中间人对象就可以间接调用服务里的方法

public void click(View v) {
		
		myBinder.callBanZheng(100);
	}

[7]当Activity销毁的时候解绑服务

@Override
	protected void onDestroy() {
		unbindService(conn);
		super.onDestroy();
		
	}

完整代码:

MainActivity.java页面

package com.dahui.banzheng;

import com.dahui.banzheng.BanZhengService.MyBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

	private MyBinder myBinder;
	private Myconn conn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		conn = new Myconn();
		Intent intent = new Intent(this,BanZhengService.class);
		bindService(intent, conn, BIND_AUTO_CREATE);
	}

	public void click(View v) {
		
		myBinder.callBanZheng(100);
	}
	
	
	//监视服务的状态
	private class Myconn implements ServiceConnection{

		

		@Override  //链接成功调用
		public void onServiceConnected(ComponentName name, IBinder service) {
			myBinder = (MyBinder) service;//获取中间人对象
		}

		@Override  //失去连接调用
		public void onServiceDisconnected(ComponentName arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	@Override
	protected void onDestroy() {
		unbindService(conn);
		super.onDestroy();
		
	}
}

activity_main.xml页面

<RelativeLayout 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="com.dahui.banzheng.MainActivity" >

    <Button
        android:onClick="click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="办证" />

</RelativeLayout>

BanZhengService.java页面

package com.dahui.banzheng;

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

public class BanZhengService extends Service {

	
	@Override
	public IBinder onBind(Intent intent) {
		//把定义的中间人对象返回
		return new MyBinder();
	}
	
	public void banZheng(int money) {
		if (money>=1000) {
			Toast.makeText(getApplicationContext(), "钱到位明天来取证!!!", 1).show();
		}else {
			Toast.makeText(getApplicationContext(), "这点钱还想办事。。。。。。。", 1).show();
		}
	}
	
	//定义中间人(IBinder)
	public class MyBinder extends Binder{
		public void callBanZheng(int money) {
			//调用办证的方法
			banZheng(money);
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值