Android11(RK3568)自定义服务制作(4)-自定义服务调用跟APP如何调用自定义服务

Android11(RK3568)自定义服务制作(3)-Hardware制作和权限管理 中完成了硬件HAL层的调用后,自定义Service也算是完成了。现在主要是实现一下怎么调用自定义Service.由于上一篇把系统服务跟未知第三方的权限都打开了,现在就可以针对两个方案来讲一下APP如何调用自建服务。

自定义系统App

首先在/vendor/firefly/apps/下创建Hello文件夹。

在其中添加AndroidManifest.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="com.sommer.hello"  
      android:versionCode="1"  
      android:versionName="1.0">  
      <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".Hello"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
      </application>  
    </manifest>  

同目录下添加Android.mk文件

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Hello
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)

在Hello.java中内容如下:

package com.sommer.hello;  
      
import com.sommer.hello.R;

import android.app.Activity;  
    import android.os.ServiceManager;  
    import android.os.Bundle;  
    import android.os.IHelloService;  
    import android.os.RemoteException;  
    import android.util.Log;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.EditText;  
      
    public class Hello extends Activity implements OnClickListener {  
        private final static String LOG_TAG = "com.sommer.hello.Hello";  
          
        private IHelloService helloService = null;  
      
        private EditText valueText = null;  
        private Button readButton = null;  
        private Button writeButton = null;  
        private Button clearButton = null;  
          
        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
      
        helloService = IHelloService.Stub.asInterface(
            ServiceManager.getService("hello"));
              
            valueText = (EditText)findViewById(R.id.edit_value);  
            readButton = (Button)findViewById(R.id.button_read);  
            writeButton = (Button)findViewById(R.id.button_write);  
            clearButton = (Button)findViewById(R.id.button_clear);  
      
        readButton.setOnClickListener(this);  
        writeButton.setOnClickListener(this);  
        clearButton.setOnClickListener(this);  
              
            Log.i(LOG_TAG, "Hello Activity Created");  
        }  
          
        @Override  
        public void onClick(View v) {  
            if(v.equals(readButton)) {  
            try {  
                    int val = helloService.getVal();  
                    String text = String.valueOf(val);  
                    valueText.setText(text);  
            } catch (RemoteException e) {  
                Log.e(LOG_TAG, "Remote Exception while reading value from device.");  
            }         
            }  
            else if(v.equals(writeButton)) {  
            try {  
                    String text = valueText.getText().toString();  
                    int val = Integer.parseInt(text);  
                    helloService.setVal(val);  
            } catch (RemoteException e) {
                   
                Log.e(LOG_TAG, "Remote Exception while writing value to device.");  
            }  
            }  
            else if(v.equals(clearButton)) {  
                String text = "";  
                valueText.setText(text);  
            }  
        }  
    }  

在layout下创建main.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:orientation="vertical" 
    	android:gravity="center">
    	<TextView 
			android:layout_width="wrap_content"
       		android:layout_height="wrap_content" 
        	android:text="@string/value">
    	</TextView>
    	<EditText 
     		android:layout_width="fill_parent"
        	android:layout_height="wrap_content" 
        	android:id="@+id/edit_value"
        	android:hint="@string/hint">
    	</EditText>
    </LinearLayout>
     <LinearLayout
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:orientation="horizontal" 
    	android:gravity="center">
    	<Button 
    		android:id="@+id/button_read"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/read">
    	</Button>
    	<Button 
    		android:id="@+id/button_write"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/write">
    	</Button>
    	<Button 
    		android:id="@+id/button_clear"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:text="@string/clear">
    	</Button>
    </LinearLayout>
</LinearLayout>

自此所有的源码都完成了。可以通过mmm指令来编译整个自定义系统App。 整个代码,从驱动到jni再到自定义服务就都打通了,完整的代码包如下:

自定义服务的源码包

Android Studio中的App调用

这个调用方法网上很多,也不再叙述了。如果不了解可以直接下载源码包查看。

AndroidStudio App调用自定义服务例程

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值