android 自定义核心服务

native service

zygote 服务


kernel->init(init.rc)->demons
zygote->dalvik VM->systemServer
runtime->serviceManager






zygote包括两部分:
c/c++层,用来产生进程给核心服务和app
java层,调用c/c++层






activityManager 活动管理器
管理activity属性和行为,管理service
示例代码:取得正在执行的
// 取得正在运行的进程
ActivityManager activityManager = (ActivityManager)


this.getSystemService(ACTIVITY_SERVICE);
// 指定最大进程数
List<RunningServiceInfo> infos =


activityManager.getRunningServices(30);
for (int i = 0; i < infos.size(); i++) {
Log.i("tag", "info" + infos.get(i).process);
}


packageManager管理应用包
// 取得已安装包
List<PackageInfo> infos = getPackageManager


().getInstalledPackages(0);
for(int i=0;i<infos.size();i++){
Log.i("tag", "package info: "+infos.get


(i).packageName);
}






windowManager视窗管理器
// WindowManager manager=(WindowManager)
// this.getSystemService(WINDOW_SERVICE);
// 取得屏幕大小
Display display = getWindowManager().getDefaultDisplay();
display.getWidth();
display.getHeight();


// 移除某个view
getWindowManager().removeViewImmediate(new View(this));
=================================================================




native Service开发要点:

c++代码:


头文件:
#include <stdint.h>
#include <sys/type.h>
#include <utils/Parcel.h>
#ifndef ANDROID_MISOO_SQRSERVICE_H
#define ANDROID_MISOO_SQRSERVICE_H
#include <utls.h>
#include <utils/Keyed/Vector.h>
#include <ui/SurfaceComposerClient.h>
namespace_android{
class SQRService:public BBinder
{
public:
static int instantiate();
virtual status_t onTransact(uint32_t,const


Parcel&,Parcel*,uint32_t);
SQRService();
virtual ~SQRService();
};
};
#endif


源文件 :
#include <utils/IServiceManager.h>
#include <utils/IPCThreadState.h>
#include <utils/RefBase.h>
#include <utils/IInterface.h>
#include <utils/Parcel.h>
#include "SQRService.h"


namespace android{
enum{
SQUARE=IBinder::FIRST_CALL_TRANSACTION,
};
int SQRService::instantiate(){
LOGE("SQRService instantiate");
int r = defaultServiceManager()->addService(
String16("misoo.sqr"), new SQRService());
LOGE("SQRService r = %d\n", r);
return r;
}
SQRService::SQRService(){
LOGV("SQRService created");
}
SQRService::~SQRService(){
LOGV("SQRService destroyed");
}
status_t SQRService::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)


{
switch(code) {
case SQUARE: {
int num = data.readInt32();
reply->writeInt32(num * num);
LOGE("onTransact::CREATE_NUM.. n=%d\n", num);
return NO_ERROR;
}
break;
default:
LOGE("onTransact::default\n");
return BBinder::onTransact(code, data, reply, flags);
}}
};




makefile文件:
LOCAL_PATH:=${call my-dir}
Include ${CLEAR_VARS}
LOCALL_SRC_FILES:=\SQRService.cpp #源文件(相对于当前的目录)
#LOCAL_MODULE:=test_exe # 最终生成可执行文件的名称
LOCAL_MODULE:=libSQRS01 #模块最终的名称
LOCAL_C_INCLUDES:=$(JNI_H_INCLUDE) #需要包含的头文件路径
#LOCAL_STATIC_LIBRARIES:= #需要连接的静态库(*.a)
LOCLA_SHARED_LIBRARIES:=\libutils #需要连接的动态库(*.so)
LOCAL_PERLINK_MODULE:=false
Include ${BUILD_SHARED_LIBRARY} # 以一个静态库的方式进行编译




添加服务:
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <utils/IPCThreadState.h>
#include <utils/ProcessState.h>
#include <utils/IServiceManager.h>
#include <utils/Log.h>
#include <private/android_filesystem_config.h>
#include "../libadd/SQRService.h"
//#include <libadd/SQRService.h>
using namespace android;
int main(int argc, char** argv){
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
LOGI("ServiceManager: %p", sm.get());
SQRService::instantiate();
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}


然后将静态库文件放到模拟器的/system/lib
addService放到模拟器的/system/bin


===========================================================
提供通用API:IBinder接口



// sqr.h
#ifndef ANDROID_MISOO_SQR_H
#define ANDROID_MISOO_SQR_H


namespace android {
class SQR {
private:
const void getSQRService();
public:
SQR();
int execute(int n);
};
}; //namespace
#endif // ANDROID_MISOO_SQR_H









// sqr.cpp
#include <utils/IServiceManager.h>
#include <utils/IPCThreadState.h>
#include "SQR.h"


namespace android {
sp<IBinder> m_ib;
SQR::SQR(){
getSQRService();
}
const void SQR::getSQRService(){
sp<IServiceManager> sm = defaultServiceManager();
m_ib = sm->getService(String16("misoo.sqr"));
LOGE("SQR:getSQRService %p\n",sm.get());
if (m_ib == 0)
LOGW("SQRService not published, waiting...");
return;
}
int SQR::execute(int n) {
Parcel data, reply;


data.writeInt32(n);
LOGE("SQR::execute\n");
m_ib->transact(0, data, &reply);
int num = reply.readInt32();
return num;
}
};


============================================================


/* com_misoo_service_sqr01.h */
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_misoo_service_sqr01 */


#ifndef _Included_com_misoo_service_sqr01
#define _Included_com_misoo_service_sqr01
#ifdef __cplusplus


extern "C" {


#endif
/*
* Class: com_misoo_service_sqr01
* Method: addTest
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_misoo_service_sqr01_execute
(JNIEnv*, jobject, jint);


#ifdef __cplusplus
}
#endif
#endif








/* com_misoo_service_sqr01.cpp */
#include "com_misoo_service_sqr01.h"
#include <utils/Log.h>
#include <utils/IPCThreadState.h>
#include <utils/ProcessState.h>
#include "../core_service/SQRService.h"
#include "SQR.h"


using namespace android;
JNIEXPORT jint JNICALL Java_com_misoo_service_sqr01_execute(JNIEnv


*env,
jobject thiz, jint x)
{
char s[] = "Java_com_misoo_service_sqr01_execute!!!";
LOGE("%s X = %d\n", s, x);
SQR* sqrObj = new SQR();


int num = sqrObj->execute(x);
return num;
}


// Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
com_misoo_service_sqr01.cpp \
SQR.cpp


base := $(LOCAL_PATH)/../../..
LOCAL_C_INCLUDES := \
$(JNI_H_INCLUDE) \
$(base)/CS01
LOCAL_SHARED_LIBRARIES := \
libutils \
libSQRS01
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE := libSQRS01_jni
include $(BUILD_SHARED_LIBRARY)


========================================


// sqr01.java
package com.misoo.service;
public class sqr01
{
static
{ System.loadLibrary("libSQRS01_jni"); }
public static native int execute(int x);
}




// SuperSQR.java
package com.misoo.service;
abstract public class SuperSQR {
public int exec(){
return sqr01.execute(onGetX());
}
abstract public int onGetX();
}




// mySQR.java
package com.misoo.pk01;
import com.misoo.service.SuperSQR;
public class mySQR extends SuperSQR {
@Override
public int onGetX() {
return 60;
}
}




// ac01.java
package com.misoo.pk01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;


public class ac01 extends Activity implements OnClickListener {
private Button btn, btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this); btn.setBackgroundResource


(R.drawable.heart);
btn.setId(101); btn.setText("run");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(120, 55);
param.topMargin = 10;
layout.addView(btn, param);


btn2 = new Button(this); btn2.setBackgroundResource


(R.drawable.ok);
btn2.setId(102); btn2.setText("exit");
btn2.setOnClickListener(this);
layout.addView(btn2, param);
setContentView(layout);
}
public void onClick(View v) {
switch(v.getId()){
case 101:
mySQR sqr = new mySQR();
int k = sqr.exec();
setTitle("Value = " + String.valueOf(k));
break;
case 102:
finish();
break;
}
}}

你可能感兴趣的:(android)