zoukankan      html  css  js  c++  java
  • Android O HIDL Android APP 实现

    一、Android app 实现:

    ① 首先创建一个空的 MainActivity 工程,并在工程中加入 AIDL 文件:

    打开 project 视图,在 main 下新建 AIDL 目录,然后新建如上图的 package,最后加入我们上一阶段实现的 aidl 接口文件

    ② 编辑 MainActivity:

     1 package com.zackary.app_001_hidldebug;
     2 
     3 import androidx.appcompat.app.AppCompatActivity;
     4 
     5 import android.content.ComponentName;
     6 import android.content.Intent;
     7 import android.content.ServiceConnection;
     8 import android.os.Bundle;
     9 import android.os.IBinder;
    10 import android.os.RemoteException;
    11 import android.util.Log;
    12 import android.view.View;
    13 import android.widget.Button;
    14 import android.widget.Toast;
    15 
    16 import android.hardware.hidldebug.IHidlDebugService;
    17 
    18 import java.lang.reflect.InvocationTargetException;
    19 import java.lang.reflect.Method;
    20 
    21 public class MainActivity extends AppCompatActivity {
    22     private static final String TAG = "HidlDebug";
    23     private IHidlDebugService mIHidlDebugService;
    24 
    25     @Override
    26     protected void onCreate(Bundle savedInstanceState) {
    27         super.onCreate(savedInstanceState);
    28         setContentView(R.layout.activity_main);
    29 
    30         hidlDebugServiceInit();
    31 
    32         Button button = (Button)findViewById(R.id.button);
    33         button.setOnClickListener(new View.OnClickListener() {
    34             @Override
    35             public void onClick(View view) {
    36                 if(mIHidlDebugService == null){
    37                     Toast.makeText(getApplicationContext(), "service null.", Toast.LENGTH_SHORT).show();
    38                     return;
    39                 }
    40                 try {
    41                     Toast.makeText(getApplicationContext(), mIHidlDebugService.hidl_debug_test("Android."),
    42                             Toast.LENGTH_SHORT).show();
    43                 } catch (RemoteException e) {
    44                     e.printStackTrace();
    45                 }
    46             }
    47         });
    48     }
    49 
    50     private void hidlDebugServiceInit() {
    51         try {
    52             Class serviceManager = Class.forName("android.os.ServiceManager");
    53             Method method = serviceManager.getMethod("getService", String.class);
    54             Object  hidlDebugService = method.invoke(serviceManager.newInstance(), "zackary.HidlDebugService");
    55             mIHidlDebugService = IHidlDebugService.Stub.asInterface((IBinder)hidlDebugService);
    56             Log.w(TAG, "Get mIHidlDebugService successed.");
    57         } catch (ClassNotFoundException e) {
    58             e.printStackTrace();
    59         } catch (NoSuchMethodException e) {
    60             e.printStackTrace();
    61         } catch (SecurityException e) {
    62             e.printStackTrace();
    63         } catch (IllegalAccessException e) {
    64             e.printStackTrace();
    65         } catch (IllegalArgumentException e) {
    66             e.printStackTrace();
    67         } catch (InvocationTargetException e) {
    68             e.printStackTrace();
    69         } catch (InstantiationException e) {
    70             e.printStackTrace();
    71         }
    72     }
    73 }

    马马虎虎实现完成,只是为了简单的验证下功能,并没有什么技术上的体现,APP 这方面的编写不是很懂,最终选用了上面的这种反射的方法,通过隐藏的类 ServiceManager 来获取到我们实现好的 service,

    然后再调用该 service 的方法,暂时没有找出其它的调用方法,在网上搜了下,好像是 Anddroid P 已禁止使用反射机制了,大概情况也没有详细的了解,总之简约而又简单。

     

    二、 测试验证

    ① Make 整个工程

     1 buildscript {
     2     repositories {
     3         google()
     4         //jcenter()
     5         maven { url 'https://maven.aliyun.com/repository/jcenter' }
     6     }
     7     dependencies {
     8         classpath "com.android.tools.build:gradle:4.0.0"
     9 
    10         // NOTE: Do not place your application dependencies here; they belong
    11         // in the individual module build.gradle files
    12     }
    13 }
    14 
    15 allprojects {
    16     repositories {
    17         google()
    18         //jcenter()
    19         maven { url 'https://maven.aliyun.com/repository/jcenter' }
    20     }
    21 }

    我这里使用的是当前最新的 4.0 版本 AS,在 make 时可能是网络的原因导致的问题,jcenter() 是不可用的,所以将 build.gradle 中的两处理都进行了相应的替换,换为 aliyun 的 jcenter 后搞定。

    ② 运行 APP

    在虚拟设备或在已连接的真实 android 设备上 run 这个测试程序,需要先手动启动下 HIDL 的服务器,然后点击按钮,查看最终的运行结果。

     

  • 相关阅读:
    【poj1655】Balancing Act
    yargs.js用法
    8、typescript
    7、typescript
    6、typescript
    5、typescript
    4、typescript
    3、typescript
    2、typescript
    1、typescript
  • 原文地址:https://www.cnblogs.com/GyForever1004/p/13741375.html
Copyright © 2011-2022 走看看