您的位置:首页 > 移动开发 > Android开发

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

2016-12-22 11:29 603 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息