美文网首页
Service AIDL总结

Service AIDL总结

作者: 梦里风吹过 | 来源:发表于2019-03-05 10:40 被阅读0次

基于Android Studio。

服务端

1. 创建aidl文件

创建文件 创建成功

示例

interface AppCheckInterface {
    boolean checkApp(String identify);
}

2. 创建Service文件。

新建为普通Java类文件,展示如下:


service

代码:

public class AppCheckService extends Service {
    final String IDENTIFY = "12345678";
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    private final AppCheckInterface.Stub binder = new AppCheckInterface.Stub() {
        @Override
        public boolean checkApp(String identify) throws RemoteException {
            return TextUtils.equals(IDENTIFY,identify);
        }
    };
}

3. 配置(重要)

        <service android:name=".AppCheckService"
                 android:enabled="true"
                 android:exported="true">
            <intent-filter>
                 <action android:name="com.ceshi.aidltest.AppCheckService" />
                <category android:name="android.intent.category.DEFAULT"/>
             </intent-filter>
        </service>

4. 本APP使用示例

class MainActivity : AppCompatActivity() {
    internal var appCheckInterface: AppCheckInterface? = null
    internal var serviceConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            appCheckInterface = AppCheckInterface.Stub.asInterface(service)

            if (appCheckInterface != null) {
                try {
                    Log.e("结果", "" + appCheckInterface!!.checkApp("12345678"))
                } catch (e: RemoteException) {
                    e.printStackTrace()
                    Log.e("结果", "出错")
                }

            } else {
                Log.e("结果", "出错  null")
            }
        }

        override fun onServiceDisconnected(name: ComponentName) {

        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent = Intent(this,AppCheckService ::class.java)
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
    }

    override fun onDestroy() {
        super.onDestroy()
        unbindService(serviceConnection)
    }
}

客户端

1. 创建aidl文件,创建过程与服务端相同,但是由于自动生成包名不同,需要改包名。

aidl文件

注意:aidl文件包名许与服务端相同,文件内容和服务端相同(可直接从服务端复制过来)

2. 使用示例:

interface AppCheckInterface {
    boolean checkApp(String identify);
}
public class MainActivity extends AppCompatActivity {

    AppCheckInterface appCheckInterface;
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            appCheckInterface = AppCheckInterface.Stub.asInterface(service);
            if (appCheckInterface!=null){
                try {
                    Log.e("结果",""+ appCheckInterface.checkApp("HhZ8yhiDrow3c3lz"));
                } catch (RemoteException e) {
                    e.printStackTrace();
                    Log.e("结果","出错");
                }
            }else {
                Log.e("结果","出错  null");
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();
        //这里action和包名需要和服务端对应。
        intent.setAction("com.ceshi.aidltest.AppCheckService");
        intent.setPackage("com.ceshi.aidltest");
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}

注:使用过程中需先安装服务端APP,客户端使用过程中,只需保证服务端已安装即可,可以不在服务端手动启动service。

相关文章

  • Service aidl 总结

    Service两种启动方式 startService 启动流程oncreate->onStartCommand->...

  • Service AIDL总结

    基于Android Studio。 服务端 1. 创建aidl文件 示例 2. 创建Service文件。 新建为普...

  • [note] IPC sketch

    1, define AIDL file 2, Server service manifest 中声明Service...

  • Service,AIDL

    服务的运行不依赖于任何用户界面,即使程序被切换到后台,或者用户打开另一个应用,服务仍然能保持独立正常运行。 服务依...

  • 2018-07-06

    service和AIDL service执行与UI进程中,所以不要在service中执行耗时操作。 service...

  • 再忆Service(二)

    Aidl && Service 0x10 onTransact方式连接 0x20 通过aidl方式连接 在main...

  • Binder 连接池

    典型的AIDL使用流程:首先创建一个Service和一个AIDL接口,接着创建一个类继承自AIDL接口中的Stub...

  • 2.5 Binder连接池

    1. aidl实现流程概述 首先建立一个aidl接口和一个Service,接着实现一个类A继承aidl接口中的St...

  • 进程间通信之AIDL解析

    在前面《Activity与Service数据交互的几种方式》文章中,讲解了AIDL是什么,AIDL的使用流程,AI...

  • Android的Service_AIDL笔记

    service: aidl: 首先创建一个AIDL_S项目,创建一个interface接口,定义接口,在把接口的后...

网友评论

      本文标题:Service AIDL总结

      本文链接:https://www.haomeiwen.com/subject/mktnuqtx.html