android服务_通过bindService调用服务里的方法

1、项目目录结构

2、activity_main.xml界面

3、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.zgs.CallServicelMethod.MainActivity" >
    <Button
        android:id="@+id/button1"
        android:onClick="click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="56dp"
        android:text="调用服务里面的方法" />
</RelativeLayout>
4、TestService.java代码
package com.zgs.CallServicelMethod;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class TestService extends Service {
	//当使用bindservice启动服务成功时会返回IBinder对象
	@Override
	public IBinder onBind(Intent intent) {
		//把我们定义的中间人对象返回
		return new MyBinder();
	}
	@Override
	public void onCreate() {
		super.onCreate();
	}
	//测试方法
	public void banZheng(){
		Toast.makeText(getApplicationContext(), "陪客户办证", 1).show();
	}
	//打麻将的方法
	public void playMaJiang(){
		System.out.println("陪客户打麻将");
	}
	//洗桑拿的方法
	public void xiShangNa(){
		System.out.println("陪客户洗桑拿");
	}
	//定义一个中间人对象
	private class MyBinder extends Binder implements Iservice{
		@Override
		public void callBanZheng() {
			banZheng();
		}
		@Override
		public void callPlayMaJiang() {
			playMaJiang();
		}
		public void callXiSangNa(){
			xiShangNa();
		}
	}
}
5、Iservice.java代码
package com.zgs.CallServicelMethod;
public interface Iservice {
	//把想暴露的方法都定义在接口里面 
	public void callBanZheng();
	public void callPlayMaJiang();
}
6、MainActivity.java代码
package com.zgs.CallServicelMethod;
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.View;
public class MainActivity extends Activity {
	private Iservice myBinder; //这个是我们定义的中间人对象
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//★★★★★注意★★★★★
		//若直接new TestService()类去调用内部的方法,是没有办法获取到context对象的
		//导致需要context对象的方法执行失败,直接new该类得到的类与普通类没有任何区别,
		//它就不在是服务了
		//开启服务
		Intent intent = new Intent(this,TestService.class);
		//连接服务 TestService
		MyConn conn = new  MyConn();
		//绑定服务 
		bindService(intent, conn, BIND_AUTO_CREATE);
	}
	//监视服务的状态
	private class MyConn implements ServiceConnection{
		//当连接服务成功后
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			//获取我们定义的中间人对象 
			myBinder = (Iservice) service;
		}
		//失去连接
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
	}
	//点击按钮,调用TestService服务里面的方法 
	public void click(View v) {
		//通过我们定义的中间人对象 间接调用服务里面的方法
		myBinder.callBanZheng();
		myBinder.callPlayMaJiang();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值