bindService:绑定本地服务和远程服务示例

bindService:绑定本地服务和远程服务示例绑定本地服务AndroidManifest.xml中声明服务:<serviceandroid:name=".TestLocalService"><intent-filter><actionandroid:name="maureen.intent.action.BIND_LOCAL…

大家好,又见面了,我是你们的朋友全栈君。

绑定本地服务

AndroidManifest.xml中声明服务:

        <service android:name=".TestLocalService">
            <intent-filter>
                <action android:name="maureen.intent.action.BIND_LOCAL_SERVICE"/>
            </intent-filter>
        </service>

TestLocalService.java

public class TestLocalService extends Service {
    private final String TAG = TestLocalService.class.getSimpleName();
    private IBinder mServiceBinder = new TestLocalServiceBinder();

    public class TestLocalServiceBinder extends Binder {
            public TestLocalService getService() {
                return TestLocalService.this;
            }
    }

    public void testFunc() {
        Log.d(TAG,"testFunc");
        Log.d(TAG, Log.getStackTraceString(new Throwable()));
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind:mServiceBinder=" + mServiceBinder);
        return mServiceBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

TestBindServiceActivity.java

  • 点击按钮绑定service
  • 绑定成功后调用TestLocalService里的testFunc方法
  • 点击back键解绑服务
public class TestBindServiceActivity extends Activity {
    private static final String TAG = TestBindServiceActivity.class.getSimpleName();
    private static final String ACTION_BIND_LOCAL_SERVICE = "maureen.intent.action.BIND_LOCAL_SERVICE";
    private Button mBindLocalServiceBtn;
    private static ServiceConnection mLocalConnection;
    private static TestLocalService mLocalService = null;

    private static class TestLocalConenction implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d(TAG,"onServiceConnected:iBinder=" + iBinder);
            mLocalService = ((TestLocalService.TestLocalServiceBinder)iBinder).getService();
            mLocalService.testFunc();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mLocalService = null;
        }
    }

    private static class ButtonClickListener implements View.OnClickListener {
        private WeakReference<TestBindServiceActivity> mActivity;
        public ButtonClickListener(TestBindServiceActivity activity) {
            mActivity = new WeakReference<>(activity);
        }
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.bind_local_service:
                    Intent localIntent = new Intent();
                    //Without this, throw exception.
                    localIntent.setPackage("com.example.maureen.mytestbindservice");
                    localIntent.setAction(ACTION_BIND_LOCAL_SERVICE);
                    mActivity.get().bindService(localIntent, mLocalConnection, BIND_AUTO_CREATE);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_test_bind_service);
        mLocalConnection = new TestLocalConenction();
        mBindLocalServiceBtn = findViewById(R.id.bind_local_service);
        mBindLocalServiceBtn.setOnClickListener(new ButtonClickListener(this));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        unbindService(mLocalConnection);
    }
}

值得注意的是,在TestLocalService中onBind函数返回的mBinder和TestBindServiceActivity中onServiceConnected中的参数iBinder是相同的:

onBind:mServiceBinder=com.example.maureen.mytestbindservice.TestLocalService$TestLocalServiceBinder@3ef6efd
onServiceConnected:iBinder=com.example.maureen.mytestbindservice.TestLocalService$TestLocalServiceBinder@3ef6efd

从Activity中调用testFunc并未通过Binder调用:

2010-01-02 04:29:32.944 5947-5947/com.example.maureen.mytestbindservice D/TestLocalService: testFunc
2010-01-02 04:29:32.950 5947-5947/com.example.maureen.mytestbindservice D/TestLocalService: java.lang.Throwable
        at com.example.maureen.mytestbindservice.TestLocalService.testFunc(TestLocalService.java:22)
        at com.example.maureen.mytestbindservice.TestBindServiceActivity$TestLocalConenction.onServiceConnected(TestBindServiceActivity.java:27)
        at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1652)
        at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1681)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:940)

绑定远程服务

  • AndroidManifest.xml中声明service,指定service运行的进程:
        <service android:name=".TestRemoteService"
                 android:process=":remote">
            <intent-filter>
                <action android:name="maureen.intent.action.BIND_REMOTE_SERVICE"/>
            </intent-filter>
        </service>
  • 编写AIDL文件- IRemoteServiceAidlInterface.aidl
interface IRemoteServiceAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void testFunc1();
    void testFunc2();
}

生成的对应的java文件-IRemoteServiceAidlInterface.java

public interface IRemoteServiceAidlInterface extends android.os.IInterface {
	/** Local-side IPC implementation stub class. */
	public static abstract class Stub extends android.os.Binder implements com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface {
		private static final java.lang.String DESCRIPTOR = "com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface";
		
		/** Construct the stub at attach it to the interface. */
		public Stub() {
			this.attachInterface(this, DESCRIPTOR);
		}
		
		/**
		 * Cast an IBinder object into an com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface interface,
		 * generating a proxy if needed.
		 */
		public static com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface asInterface(android.os.IBinder obj) {
			if ((obj==null)) {
				return null;
			}
			ndroid.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
			if (((iin!=null)&&(iin instanceof com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface))) {
				return ((com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface)iin);
			}
			return new com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface.Stub.Proxy(obj);
		}
		
		@Override 
		public android.os.IBinder asBinder() {
			return this;
		}
		
		@Override 
		public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
			java.lang.String descriptor = DESCRIPTOR;
			switch (code) {
				case INTERFACE_TRANSACTION: {
					reply.writeString(descriptor);
					return true;
				}
				
				case TRANSACTION_testFunc1: {
					data.enforceInterface(descriptor);
					this.testFunc1();
					reply.writeNoException();
					return true;
				}
				
				case TRANSACTION_testFunc2: {
					data.enforceInterface(descriptor);
					this.testFunc2();
					reply.writeNoException();
					return true;
				}
				
				default: {
					return super.onTransact(code, data, reply, flags);
				}
			}
		}
		
		private static class Proxy implements com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface {
			private android.os.IBinder mRemote;
			Proxy(android.os.IBinder remote) {
				mRemote = remote;
			}
			
			@Override 
			public android.os.IBinder asBinder() {
				return mRemote;
			}
			
			public java.lang.String getInterfaceDescriptor() {
				return DESCRIPTOR;
			}
			
			/**
			 * Demonstrates some basic types that you can use as parameters
			 * and return values in AIDL.
			 */
			@Override 
			public void testFunc1() throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					mRemote.transact(Stub.TRANSACTION_testFunc1, _data, _reply, 0);
					_reply.readException();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
			}
			
			@Override 
			public void testFunc2() throws android.os.RemoteException {
				android.os.Parcel _data = android.os.Parcel.obtain();
				android.os.Parcel _reply = android.os.Parcel.obtain();
				try {
					_data.writeInterfaceToken(DESCRIPTOR);
					mRemote.transact(Stub.TRANSACTION_testFunc2, _data, _reply, 0);
					_reply.readException();
				} finally {
					_reply.recycle();
					_data.recycle();
				}
			}
		}
		
		static final int TRANSACTION_testFunc1 = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
		static final int TRANSACTION_testFunc2 = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
	}
	
	/**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
	public void testFunc1() throws android.os.RemoteException;
	public void testFunc2() throws android.os.RemoteException;
}
  • TestRemoteService.java
public class TestRemoteService extends Service {
    private final String TAG= TestRemoteService.class.getSimpleName();
    private IBinder mRemoteBinder = new RemoteServiceImpl(this);

    private static class RemoteServiceImpl extends IRemoteServiceAidlInterface.Stub {
        private final String TAG= RemoteServiceImpl.class.getSimpleName();
        private WeakReference<TestRemoteService> mRemoteService;
        public RemoteServiceImpl(TestRemoteService service) {
            mRemoteService = new WeakReference<>(service);
        }
        @Override
        public void testFunc1() {
            Log.d(TAG,"testFunc1");
            Log.d(TAG,Log.getStackTraceString(new Throwable()));
            mRemoteService.get().testMyFunc1();
        }

        @Override
        public void testFunc2() {
            Log.d(TAG,"testFunc2");
            mRemoteService.get().testMyFunc2();
        }
    }

    private void testMyFunc1() {
        Log.d(TAG,"testMyFunc1");
        Log.d(TAG,Log.getStackTraceString(new Throwable()));
    }

    private void testMyFunc2() {
        Log.d(TAG,"testMyFunc2");
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG,"onCreate");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"onBind:mRemoteBinder=" + mRemoteBinder);
        return mRemoteBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG,"onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }
}
  • TestBindServiceActivity.java

       ① 点击按钮,绑定远程服务

       ②服务绑定成功后,调用aidl中声明的方法testFunc1和testFunc2

      ③点击back键,解绑服务

package com.example.maureen.mytestbindservice;

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.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.lang.ref.WeakReference;

public class TestBindServiceActivity extends Activity {
    private static final String TAG = TestBindServiceActivity.class.getSimpleName();

    private static final String ACTION_BIND_REMOTE_SERVICE = "maureen.intent.action.BIND_REMOTE_SERVICE";
    private Button mBindRemoteServiceBtn;
    private static ServiceConnection mRemoteConnection;


    private static class TestRemoteConection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d(TAG,"onServiceConnected:iBinder=" + iBinder);
            IRemoteServiceAidlInterface remoteService = IRemoteServiceAidlInterface.Stub.asInterface(iBinder);
            try {
                remoteService.testFunc1();
                remoteService.testFunc2();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }

    private static class ButtonClickListener implements View.OnClickListener {
        private WeakReference<TestBindServiceActivity> mActivity;
        public ButtonClickListener(TestBindServiceActivity activity) {
            mActivity = new WeakReference<>(activity);
        }
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.bind_remote_service:
                    Intent remoteIntent = new Intent();
                    //Without this, throw exception.
                    remoteIntent.setPackage("com.example.maureen.mytestbindservice");
                    remoteIntent.setAction(ACTION_BIND_REMOTE_SERVICE);
                    mActivity.get().bindService(remoteIntent, mRemoteConnection, BIND_AUTO_CREATE);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_test_bind_service);
        mRemoteConnection = new TestRemoteConection();
        mBindRemoteServiceBtn = findViewById(R.id.bind_remote_service);
        mBindRemoteServiceBtn.setOnClickListener(new ButtonClickListener(this));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        unbindService(mRemoteConnection);
    }
}

值得注意的是TestRemoteService.java的onBind函数中返回的mRemoteBinder是一个Binder服务端对象,即RemoteServiceImpl对象:

nBind:mRemoteBinder=com.example.maureen.mytestbindservice.TestRemoteService$RemoteServiceImpl@3920201

而在TestBindServiceActivity的TestRemoteServiceConnection的onServiceConnected函数中iBinder是Binder代理端:

onServiceConnected:iBinder=android.os.BinderProxy@6ff13d8

即Activity调用到TestRemoteService是通过Binder调用完成的。

例如TestRemoteService.RemoteServiceImpl.testFunc1调用堆栈:

2010-01-02 05:05:47.136 6482-6495/com.example.maureen.mytestbindservice:remote D/RemoteServiceImpl: testFunc1
2010-01-02 05:05:47.140 6482-6495/com.example.maureen.mytestbindservice:remote D/RemoteServiceImpl: java.lang.Throwable
        at com.example.maureen.mytestbindservice.TestRemoteService$RemoteServiceImpl.testFunc1(TestRemoteService.java:24)
        at com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface$Stub.onTransact(IRemoteServiceAidlInterface.java:51)
        at android.os.Binder.execTransact(Binder.java:697)

而TestRemoteService.testFunc1调用堆栈:

2010-01-02 05:05:47.140 6482-6495/com.example.maureen.mytestbindservice:remote D/TestRemoteService: testMyFunc1
2010-01-02 05:05:47.143 6482-6495/com.example.maureen.mytestbindservice:remote D/TestRemoteService: java.lang.Throwable
        at com.example.maureen.mytestbindservice.TestRemoteService.testMyFunc1(TestRemoteService.java:37)
        at com.example.maureen.mytestbindservice.TestRemoteService.access$000(TestRemoteService.java:11)
        at com.example.maureen.mytestbindservice.TestRemoteService$RemoteServiceImpl.testFunc1(TestRemoteService.java:25)
        at com.example.maureen.mytestbindservice.IRemoteServiceAidlInterface$Stub.onTransact(IRemoteServiceAidlInterface.java:51)
        at android.os.Binder.execTransact(Binder.java:697)

源码位置

https://github.com/snowdrift1993/Android/tree/master/MyTestBindService

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/134179.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 分析开源项目[通俗易懂]

    分析开源项目[通俗易懂]手把手教你分析开源项目不知道代码怎么来的?代码跑不起来?项目对自己有帮助,不会模块化分析?任何一个开源项目,都可以让自己得到提升!这里以EL-ADMIN为例https://el-admin.vip/1、观察开源项目后端代码:前端代码:2、开源项目下载可使用zip下载,或者Git观察:用了哪些技术(springboot,vue,redis,)是否有数据库(mysql)你的环境是否匹配(Java,Maven,npm,nodejs,)通过了,然后再想办法运行3、跑

  • 方差、协方差、标准差、均方差、均方根值、均方误差、均方根误差对比分析[通俗易懂]

    方差、协方差、标准差、均方差、均方根值、均方误差、均方根误差对比分析[通俗易懂]方差、协方差、标准差(标准偏差/均方差)、均方误差、均方根误差(标准误差)、均方根值本文由博主经过查阅网上资料整理总结后编写,如存在错误或不恰当之处请留言以便更正,内容仅供大家参考学习。 方差(Variance) 方差用于衡量随机变量或一组数据的离散程度,方差在在统计描述和概率分布中有不同的定义和计算公式。①概率论中方差用来度量随机变量和其数学期望(即均值)之间的偏…

  • httprunner(10)日志信息「建议收藏」

    httprunner(10)日志信息「建议收藏」日志信息每个用例都会生成一个对应的log日志,位置:<ProjectRootDir>/logs/TestCaseID.run.log.如果你想看到request和response、提取

  • PHPMailer发送邮件失败:SMTP connect failed

    PHPMailer发送邮件失败:SMTP connect failed

  • hotumoyi吉他_木棒能做什么

    hotumoyi吉他_木棒能做什么乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过 50 个长度单位。然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度。请你设计一个程序,帮助乔治计算木棒的可能最小长度。每一节木棍的长度都用大于零的整数表示。输入格式输入包含多组数据,每组数据包括两行。第一行是一个不超过 64 的整数,表示砍断之后共有多少节木棍。第二行是截断以后,所得到的各节木棍的长度。在最后一组数据之后,是一个零。输出格式为每组数据,分别输出原始木棒的可能最小长度

  • AWS 物联网架构 以及 AWS Greengrass 的介绍[通俗易懂]

    AWS 物联网架构 以及 AWS Greengrass 的介绍[通俗易懂]1、AWS的物联网架构AmazonWebServices(AWS)是一个安全的云服务平台,提供计算能力、数据库存储、内容交付以及其他功能来帮助实现业务扩展和增长。了解数以百万计的客户目前如何利用AWS云产品和解决方案来构建灵活性、可扩展性和可靠性更高的复杂应用程序。AWSIOT主要是解决物联网相关问题的,AWSIOT的基本架构如下:AWSIOT的架构主要由三大部分组成,分别在边缘端和云端。Edge:things数据产生的来源,也是经过数…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号