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

来源:互联网 发布:淘宝客推广爆款的技巧 编辑:程序博客网 时间:2024/05/16 10:35

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对象@Overridepublic IBinder onBind(Intent intent) {//把我们定义的中间人对象返回return new MyBinder();}@Overridepublic 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{@Overridepublic void callBanZheng() {banZheng();}@Overridepublic 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; //这个是我们定义的中间人对象@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//★★★★★注意★★★★★//若直接new TestService()类去调用内部的方法,是没有办法获取到context对象的//导致需要context对象的方法执行失败,直接new该类得到的类与普通类没有任何区别,//它就不在是服务了//开启服务Intent intent = new Intent(this,TestService.class);//连接服务 TestServiceMyConn conn = new  MyConn();//绑定服务 bindService(intent, conn, BIND_AUTO_CREATE);}//监视服务的状态private class MyConn implements ServiceConnection{//当连接服务成功后@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//获取我们定义的中间人对象 myBinder = (Iservice) service;}//失去连接@Overridepublic void onServiceDisconnected(ComponentName name) {}}//点击按钮,调用TestService服务里面的方法 public void click(View v) {//通过我们定义的中间人对象 间接调用服务里面的方法myBinder.callBanZheng();myBinder.callPlayMaJiang();}}


0 0
原创粉丝点击