zoukankan      html  css  js  c++  java
  • 24 服务AIDL

    AIDL远程访问服务的方法

    • 创建一个接口类写上方法 然后修改后缀java为aidl
    • 在服务中创建一个类继承Stub类
    • 在远程访问服务的进程把AIDL文件复制(包名不能改变)
      XXXX.Stub.asInterface(service);即可

    服务端结构

    这里写图片描述

    MainActivity.java

    无实际代码

    MyService.java

    package com.qf.day24_aidl_server;
    
    import com.qf.day24_aidl_server.MyInterface.Stub;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class MyService extends Service{
    
        @Override
        public IBinder onBind(Intent intent) {
            return new MyBinder();
        }
        //Stub extends android.os.Binder implements com.qf.day24_aidl_server.MyInterface
        public class MyBinder extends Stub{
    
            @Override
            public String getServiceData() {
                return name();
            }
    
        }
    
        //服务里的方法
        public String name(){
            return "田师傅红烧肉";
        }
    
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            return super.onUnbind(intent);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
    }
    

    MyInterface.aidl

    package com.qf.day24_aidl_server;
    
     interface MyInterface {
    
         String getServiceData();
    
    }
    

    清单文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.qf.day24_aidl_server"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.qf.day24_aidl_server.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <service android:name=".MyService">
                <intent-filter >
                    <action android:name="com.qf.day24_aidl_server.MyService"/>
                </intent-filter>
            </service>
        </application>
    
    </manifest>
    

    客户端

    这里写图片描述

    MainActivity.java

    package com.qf.day24_aidl_client;
    
    
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Context;
    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 com.qf.day24_aidl_server.MyInterface;
    
    public class MainActivity extends Activity {
    
        private MyInterface myInterface;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Intent intent = new Intent("com.qf.day24_aidl_server.MyService");
            //6.0必须设置
            intent.setPackage("com.qf.day24_aidl_server");
    
    
            bindService(intent, new MyServiceConn(), Context.BIND_AUTO_CREATE);
    
        }
    
        //点击 获取Service里数据
        public void MyClick(View v){
            String name;
            try {
                name = myInterface.getServiceData();
                Log.e("AAA", "===>"+name);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
        }
    
    
    
        public class MyServiceConn implements ServiceConnection{
    
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                // TODO Auto-generated method stub
                myInterface = MyInterface.Stub.asInterface(service);
                //myInterface = myBinder;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
                // TODO Auto-generated method stub
    
            }
    
        }
    }
    

    MyInterface.aidl

    package com.qf.day24_aidl_server;
    
     interface MyInterface {
    
         String getServiceData();
    
    }
    
  • 相关阅读:
    Java 处理cookie的方法
    HTML5的新标签-整体布局
    Git学习文档——文件状态git status
    Css中路径data用法
    python2
    hangfire
    Nginx系列~Nginx服务启动不了
    git形成本地仓库并从远处url拉取
    orcal和sql server中的字符串查找函数
    Eclipse 修改项目名称
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152181.html
Copyright © 2011-2022 走看看