zoukankan      html  css  js  c++  java
  • ANDROID笔记:AIDL介绍

    package com.example.android_server.aidlService;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.RemoteException;
    
    import com.example.android_server.aidlService.IStudent.Stub;
    
    public class MyAIDLService extends Service {
        StudentBinder binder=null;
        @Override
        public void onCreate() {
            super.onCreate();
            binder=new StudentBinder();
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return binder;
        }
    
        /**
         * 自定义一个Binder,继承存根类
         * 
         * @author Administrator
         * 
         */
        public class StudentBinder extends Stub {
    
            @Override
            public int getCount() throws RemoteException {
                return 1;
            }
        }
    }

    IStudent.aidl

    package com.example.android_server.aidlService;
    interface IStudent
    {
    int getCount();
    }

    在manifest中注册服务:

     <service android:name="com.example.android_server.aidlService.MyAIDLService" >
                <intent-filter>
                    <action android:name="com.example.aidlservice" />
                </intent-filter>
            </service>

    另一个项目中的调用该Service

    package com.example.android_serviceclient;
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Message;
    import android.os.RemoteException;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.example.android_server.aidlService.IStudent;
    import com.example.android_server.aidlService.IStudent.Stub;
    
    public class MainActivity extends Activity implements Handler.Callback {
        private IStudent iStudent;
        private TextView textView=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textView=(TextView) findViewById(R.id.text);
            
            Intent intent = new Intent();
            intent.setAction("com.example.aidlservice");
            startService(intent);
            bindService(intent, connection, BIND_AUTO_CREATE);
            
            Handler handler = new Handler(this);
            handler.sendEmptyMessageDelayed(1, 2000);
    
        }
        private ServiceConnection connection = new ServiceConnection() {
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
    
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                System.out.println("onServiceConnected");
                iStudent=Stub.asInterface(service);
            }
        };
    
        @Override
        public boolean handleMessage(Message msg) {
            try {
                textView.setText(iStudent.getCount() +"");
                Toast.makeText(MainActivity.this, "得到的值:"+iStudent.getCount() + "", 200)
                        .show();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            return false;
        }
    }

    aidl是Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口.

  • 相关阅读:
    520了,用32做个简单的小程序
    年轻就该多尝试,教你20小时Get一项新技能
    自定义注解!绝对是程序员装逼的利器!!
    vs2015添加ActiveX Control Test Container工具(转载)
    编译MapWinGis
    C#遍历集合与移除元素的方法
    c#winform程序,修改MessageBox提示框中按钮的文本
    java程序员面试答题技巧
    什么是DOM
    uml类关系
  • 原文地址:https://www.cnblogs.com/afluy/p/3422863.html
Copyright © 2011-2022 走看看