如何添加一个Application Framework Service(一)(without native code)

本文介绍如何在Android系统中创建一个无需本地代码的应用框架服务(Application Framework Service)。通过定义AIDL接口、实现服务类、注册服务等步骤,最终实现在应用中调用该服务。

如何添加一个Application Framework Service(without native code)?


1、本文参照AlarmManagerService实现一个简单的Application Framework Service,取名为HelloWorldService,类图如下:



2、在目录frameworks/base/core/java/android/app/下添加aidl文件:

IHelloWorld.aidl:

package android.app;

import java.lang.String;

interface IHelloWorld

{

        void printHelloWorld();

        void printString(String string);

}

需要注意:

(1) 该文件的package声明应为:package android.app;


3、在目录frameworks/base/services/java/com/android/server/下添加文件:

HelloWorldService.java:

package com.android.server;

import android.app.IHelloWorld;

import android.content.Context;

import android.os.RemoteException;

import android.util.Log;

public class HelloWorldService extends IHelloWorld.Stub

{

        private static final String TAG = "HelloWorldService";

        private Context mContext = null;

        public HelloWorldService(Context context)

        {

                mContext = context;

        }

        @Override

        public void printHelloWorld() throws RemoteException

        {

                // TODO Auto-generated method stub

                Log.d(TAG, "Hello World!");

        }

        @Override

        public void printString(String string) throws RemoteException

        {

                // TODO Auto-generated method stub

                Log.d(TAG, string);

        }

}

需要注意:

(1)该文件的package声明应为:package com.android.server;

(2)该文件中添加语句:import android.app.IHelloWorld;


4、在目录frameworks/base/core/java/android/app/下添加文件:

HelloWorldManager.java:

package android.app;

import android.os.RemoteException;

public class HelloWorldManager

{

        private static final String TAG = "HelloWorldManager";

        private IHelloWorld mService = null;

        HelloWorldManager(IHelloWorld service)

        {

                mService = service;

        }

        public void printHelloWorld()

        {

                try

                {

                        mService.printHelloWorld();

                }

                catch(RemoteException e)

                {

                        e.printStackTrace();

                }

        }

        public void printString(String string)

        {

                try

                {

                        mService.printString(string);

                }

                catch(RemoteException e)

                {

                        e.printStackTrace();

                }

        }

}

需要注意:

(1)该文件的package声明应为:package android.app,需要与aidl文件中的package声明保持一致;


5、修改frameworks/base/下的Android.mk文件:

在“LOCAL_SRC_FILES += \”中,参照IAlarmManager的添加方式:

core/java/android/app/IAlarmManager.aidl \

在“LOCAL_SRC_FILES += \”的最后面添加:

core/java/android/app/IHelloWorld.aidl,

需要注意:

(1) 在core/java/android/app/IHelloWorld.aidl上面一行的最后面,还需要添加“\”;


6、修改frameworks/base/core/java/android/content/下的Context.java文件:

参照ALARM_SERVICE的添加方式:

public static final String ALARM_SERVICE = "alarm";


为Context 类添加成员变量:

public static final String HELLOWORLD_SERVICE = "helloworld";


7、修改frameworks/base/services/java/com/android/server/下的SystemServer.java文件:

参照AlarmManagerService的注册方式:

AlarmManagerService alarm = null;


Slog.i(TAG, "Alarm Manager");

alarm = new AlarmManagerService(context);

ServiceManager.addService(Context.ALARM_SERVICE, alarm);


将HelloWorldService注册到ServiceManager中:

HelloWorldService helloworld = null;


Slog.i(TAG, "HelloWorld Manager");

helloworld = new HelloWorldService(context);

ServiceManager.addService(Context.HELLOWORLD_SERVICE, helloworld);


8、修改frameworks/base/core/java/android/app/下的ContextImpl.java文件:

Android 4.0 ICS系统的注册方式:

参照ALARM_SERVICE的注册方式:

registerService(ALARM_SERVICE, new StaticServiceFetcher() 

{

public Object createStaticService() 

{

                    IBinder b = ServiceManager.getService(ALARM_SERVICE);

                    IAlarmManager service = IAlarmManager.Stub.asInterface(b);

                    return new AlarmManager(service);

       }

}

);


注册HELLOWORLD_SERVICE:

registerService(HELLOWORLD_SERVICE, new StaticServiceFetcher() 

{

 public Object createStaticService() 

{

                    IBinder b = ServiceManager.getService(HELLOWORLD_SERVICE);

                    IHelloWorld service = IHelloWorld.Stub.asInterface(b);

                    return new HelloWorldManager(service);

     }

}

);


Android 2.3 gingerbread系统的注册方式:

参照ALARM_SERVICE的注册方式:

private static AlarmManager sAlarmManager;


private AlarmManager getAlarmManager()

{

       synchronized (sSync) 

       {

             if (sAlarmManager == null)

   {

                 IBinder b = ServiceManager.getService(ALARM_SERVICE);

                 IAlarmManager service = IAlarmManager.Stub.asInterface(b);

                 sAlarmManager = new AlarmManager(service);

             }

         }

         return sAlarmManager;

      }


else if (ALARM_SERVICE.equals(name))

 {

  return getAlarmManager();

 }


注册HELLOWORLD_SERVICE:

private static HelloWorldManager sHelloWorldManager;


private HelloWorldManager getHelloWorldManager()

{

synchronized (sSync)

        {

       if (sHelloWorldManager == null) 

  {

             IBinder b = ServiceManager.getService(HELLOWORLD_SERVICE);

                 IHelloWorld service = IHelloWorld.Stub.asInterface(b);

                 sHelloWorldManager = new HelloWorldManager(service);

            }

     }

     return sHelloWorldManager;

}


else if (HELLOWORLD_SERVICE.equals(name)) 

{

return getHelloWorldManager();


9、如果Service中不涉及到JNI操作,Application Framework Service HelloWorldService就添加完成,

然后就可以直接在 Application中像使用AlarmManager一样使用HelloWorldManager进行操作,

示例如下:

HelloWorldServiceTest.java

package com.android.example;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.app.HelloWorldManager;

import android.content.Context;

public class HelloWorldServiceTest extends Activity 

{

private static final String TAG = "HelloWorldServiceTest";

private HelloWorldManager mHelloWorldManager = null;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        mHelloWorldManager = (HelloWorldManager)getSystemService(Context.HELLOWORLD_SERVICE);

        

        Log.d(TAG, "******************************");

        mHelloWorldManager.printHelloWorld();

        Log.d(TAG, "******************************");

        mHelloWorldManager.printString("I succeed!");

        Log.d(TAG, "******************************");

    }

}


Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_PACKAGE_NAME := HelloWorldServiceTest

include $(BUILD_PACKAGE)


程序执行结果如下图:

### Android Native UI Frameworks and Libraries In the context of developing applications for Android, a variety of native UI frameworks and libraries are available to enhance user interface creation. One such framework is described as driving off the UI of both Android native and hybrid developments[^1]. This indicates that there exists an integrated approach where developers can leverage components designed specifically for enhancing or customizing the look and feel of their application's interfaces. For those looking into customization at deeper levels within the system, efforts have been made towards transplanting `Android SystemUI` applications which not only aids in understanding how one might customize aspects of the device’s appearance but also sets up groundwork for future customized applications[^2]. When it comes to integrating rich messaging features directly through React Native projects targeting iOS devices, RCTAuroraIMUI has emerged as a solution by providing pre-built functionalities packaged neatly inside `.framework`, facilitating easier inclusion of chat-related capabilities without needing extensive coding from scratch[^3]. However, when considering ease alongside versatility across platforms including web technologies (HTML/CSS), some prefer using cross-platform tools like Xamarin due to its ability to achieve near-native performance while allowing code sharing between different operating systems; yet opinions vary on whether this outweighs simplicity offered by hybrids built primarily around JavaScript-based stacks[^4]. #### Example Code Snippet Demonstrating Use of Custom View in XML Layout File ```xml <com.example.customviews.CustomButton android:id="@+id/custom_button" android:layout_width="wrap_content" android:layout_height="wrap_content" app:textColor="#FFFFFF"/> ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值