【Quote】 Android-Adding SystemService

From http://processors.wiki.ti.com/index.php/Android-Adding_SystemService

Android-Adding SystemService

This wiki page will demonstrate - "How to add system service to android framework". Example - "Adding a Bluetooth HID service" - taken as reference of understanding.This will also help to add support for more bluetooth profiles into android framework.

Contents

[hide]

What is service?

As per the definition given at http://developer.android.com/guide/topics/fundamentals/services.html

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

Service layer

Create service

  • Add your code to frameworks/base/services/java/com/android/server/
/*TestService.java */
package com.android.server;
import android.content.Context;
import android.os.Handler;
import android.os.ITestService;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
public class TestService extends ITestService.Stub {
    private static final String TAG = "TestService";
    private TestWorkerThread mWorker;
    private TestWorkerHandler mHandler;
    private Context mContext;
    public TestService(Context context) {
        super();
        mContext = context;
        mWorker = new TestWorkerThread("TestServiceWorker");
        mWorker.start();
        Log.i(TAG, "Spawned worker thread");
    }
 
    public void setValue(int val) {
        Log.i(TAG, "setValue " + val);
        Message msg = Message.obtain();
        msg.what = TestWorkerHandler.MESSAGE_SET;
        msg.arg1 = val;
        mHandler.sendMessage(msg);
    }
 
    private class TestWorkerThread extends Thread {
        public TestWorkerThread(String name) {
            super(name);
        }
        public void run() {
            Looper.prepare();
            mHandler = new TestWorkerHandler();
            Looper.loop();
        }
    }
 
    private class TestWorkerHandler extends Handler {
        private static final int MESSAGE_SET = 0;
        @Override
        public void handleMessage(Message msg) {
            try {
                if (msg.what == MESSAGE_SET) {
                    Log.i(TAG, "set message received: " + msg.arg1);
                }
            } catch (Exception e) {
                // Log, don‘t crash!
                Log.e(TAG, "Exception in TestWorkerHandler.handleMessage:", e);
            }
        }
    }
}

Register service

  • Register service in SystemServer.java
/*
 * go to function "@Override public void run()"
 * ........
 * Add following block after line "if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {"
 */
 
try {
    Slog.i(TAG, "Test Service");
    ServiceManager.addService(“Test”, new TestService(context));
} catch (Throwable e) {
    Slog.e(TAG, "Failure starting TestService Service", e);
}

Expose service

  • A service can expose set of functions that can be access by other process/application. Exposed functions are required to be declared in .aidl file at following location

frameworks/base/core/java/android/os/[server].aidl

/*
* aidl file : frameworks/base/core/java/android/os/ITestService.aidl
* This file contains definitions of functions which are exposed by service
*/
package android.os;
interface ITestService {
/**
* {@hide}
*/
	void setValue(int val);
}

Add [service].aidl for build

/*
 * open frameworks/base/Android.mk and add following line
 */
...
core/java/android/os/IPowerManager.aidl core/java/android/os/ITestService.aidl core/java/android/os/IRemoteCallback.aidl ...
  • Rebuild the framework/base or android system.Service is now ready to use by other application/process.

Using service

To use service

  • first get service handle using "ServiceManager.getService()" api
  • use service handle to call set of functions exposed by service

Below is the sample code to use service.

/*
 * HelloServer.java
 */
package com.Test.helloserver;
import android.app.Activity;
import android.os.Bundle;
import android.os.ServiceManager;
import android.os.ITestService;
import android.util.Log;
public class HelloServer extends Activity {
    private static final String DTAG = "HelloServer";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        ITestService om = ITestService.Stub.asInterface(ServiceManager.getService("Test"));
        try {
            Log.d(DTAG, "Going to call service");
            om.setValue(20);
            Log.d(DTAG, "Service called succesfully");
        }
        catch (Exception e) {
            Log.d(DTAG, "FAILED to call service");
            e.printStackTrace();
        }
    }
}

References

Support

For community support join http://groups.google.com/group/rowboat 
For IRC #rowboat on irc.freenode.net

时间: 2024-10-09 16:40:45

【Quote】 Android-Adding SystemService的相关文章

【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

译者地址:[翻]Android Design Support Library 的 代码实验--几行代码,让你的 APP 变得花俏 原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fancy with few lines of code 原文项目 demo: Lab-Android-DesignLibrary 双语对照地址: [翻-双语]Android D

【原创】android——SQLite实现简单的注册登陆(已经美化)

1,Main_activity的xmL配置 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_pa

【资源】Android学习资料 - 逆天整理 - 精华无密版

 入门看视频,提高看书籍,飘升做项目.老练研开源,高手读外文,大牛讲低调~  极客学院安卓Android全套最新视频教程[17G全套视频+独家源码] http://pan.baidu.com/s/1kT5nSkn 链接: http://pan.baidu.com/s/1jGsyJ0y 密码: btbg 传智播客Java安卓方向就业班全套视频下载 http://pan.baidu.com/s/1eQq4YXG 链接: http://pan.baidu.com/s/1qWA3yyS 密码: p7qj

【整理】Android中的gravity和layout_gravity区别

[背景] 在Android中,想要设置个按钮的水平对齐,都累死了: [已解决]ADT中已设置TableLayout布局的情况下如何设置按钮居中对齐    所以现在有必要搞清楚,到底gravity和layout_gravity到底有啥区别. 1.参考: Android – gravity and layout_gravity Android中gravity与layout_gravity的区别 中的解释,可以总结为: android:gravity : 表示当前View,即控件,内部的东西的,对齐方

【原创】android——Tabhost 自定义tab+底部实现+intent切换内容

1,实现tabhost自定义格式,再此仅仅显示背景和文字,效果图预览:(底边栏所示) (图片变形) 2,xml配置 activity_user的XML配置  1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/tabhost&qu

【转】Android类动态加载技术

http://www.blogjava.net/zh-weir/archive/2011/10/29/362294.html Android应用开发在一般情况下,常规的开发方式和代码架构就能满足我们的普通需求.但是有些特殊问题,常常引发我们进一步的沉思.我们从沉思中产生顿悟,从而产生新的技术形式. 如何开发一个可以自定义控件的Android应用?就像eclipse一样,可以动态加载插件:如何让Android应用执行服务器上的不可预知的代码?如何对Android应用加密,而只在执行时自解密,从而防

【分享】Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件

[分享]Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件 在Andoird Studio下编译java文件时,经常会出现像下面的错误: Error:(29, 43) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 在这里,分享一个工具:ANSI文件批量另存为无BOM的UTF-8文件: 把下面代码用记事本存为AndroidStudioJava编码.vbs,双击即可使用: on error resume next Set WshShell=WScrip

【原创】android——SQLite的cmd命令的基本操作

步骤:enter为按键 1,开始—>运行—>输入cmd  (enter) 2,输入adb shell  (enter) 3, cd data/data/应用包名/databases  (enter) 4, ls (查看目录下的数据库)   (enter) 5,sqlite3  数据库名.db;   (enter) 6,SQL语句操作表,注意标点符号一定是在英文输入法下 7,示例: [原创]android--SQLite的cmd命令的基本操作,布布扣,bubuko.com

【随想】android是个什么东西,andorid机制随想

优秀程序员的天性就是好奇,软件是怎么运作的.屏幕是如何显示的.桌面窗体为何能如此人性化的被鼠标拖动?如果你经常会有这样一些问题迸发在脑海中,恭喜你,你是一名很有潜力的程序员. 我在大学读的是自动化专业,属于电子类,再者对计算机相当感兴趣,第一次看到这玩意时,就觉得这东西太神奇了(其实当时只要看到有屏幕的东西,都觉得很神奇).硬件+软件的深入让我直接打通了了解这一神秘机器的任督二脉.软件我不是最牛逼的,硬件其实我很歇菜.但是透过硬件看软件估计我还有那么一点点发言权.下面相关内容是个人小小的感性认识

【转】android动画之Tween动画 (渐变、缩放、位移、旋转)

原文:http://blog.csdn.net/feng88724/article/details/6318430 Android 平台提供了两类动画. 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转.平移.放缩和渐变). 第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似. 下面就讲一下Tweene Animations. 主要类: Animation   动画 AlphaAnimation 渐变透明度 RotateAnimation