android 自定义核心服务
native service
zygote服务
kernel->init(init.rc)->demons
zygote->dalvikVM->systemServer
runtime->serviceManager
zygote包括两部分:
c/c++层,用来产生进程给核心服务和app
java层,调用c/c++层
activityManager活动管理器
管理activity属性和行为,管理service
示例代码:取得正在执行的
//取得正在运行的进程
ActivityManageractivityManager=(ActivityManager)
this.getSystemService(ACTIVITY_SERVICE);
//指定最大进程数
List<RunningServiceInfo>infos=
activityManager.getRunningServices(30);
for(inti=0;i<infos.size();i++){
Log.i("tag","info"+infos.get(i).process);
}
packageManager管理应用包
//取得已安装包
List<PackageInfo>infos=getPackageManager
().getInstalledPackages(0);
for(inti=0;i<infos.size();i++){
Log.i("tag","packageinfo:"+infos.get
(i).packageName);
}
windowManager视窗管理器
//WindowManagermanager=(WindowManager)
//this.getSystemService(WINDOW_SERVICE);
//取得屏幕大小
Displaydisplay=getWindowManager().getDefaultDisplay();
display.getWidth();
display.getHeight();
//移除某个view
getWindowManager().removeViewImmediate(newView(this));
=================================================================
nativeService开发要点:
c++代码:
头文件:
#include<stdint.h>
#include<sys/type.h>
#include<utils/Parcel.h>
#ifndefANDROID_MISOO_SQRSERVICE_H
#defineANDROID_MISOO_SQRSERVICE_H
#include<utls.h>
#include<utils/Keyed/Vector.h>
#include<ui/SurfaceComposerClient.h>
namespace_android{
classSQRService:publicBBinder
{
public:
staticintinstantiate();
virtualstatus_tonTransact(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"
namespaceandroid{
enum{
SQUARE=IBinder::FIRST_CALL_TRANSACTION,
};
intSQRService::instantiate(){
LOGE("SQRServiceinstantiate");
intr=defaultServiceManager()->addService(
String16("misoo.sqr"),newSQRService());
LOGE("SQRServicer=%d\n",r);
returnr;
}
SQRService::SQRService(){
LOGV("SQRServicecreated");
}
SQRService::~SQRService(){
LOGV("SQRServicedestroyed");
}
status_tSQRService::onTransact(
uint32_tcode,constParcel&data,Parcel*reply,uint32_tflags)
{
switch(code){
caseSQUARE:{
intnum=data.readInt32();
reply->writeInt32(num*num);
LOGE("onTransact::CREATE_NUM..n=%d\n",num);
returnNO_ERROR;
}
break;
default:
LOGE("onTransact::default\n");
returnBBinder::onTransact(code,data,reply,flags);
}}
};
makefile文件:
LOCAL_PATH:=${callmy-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>
usingnamespaceandroid;
intmain(intargc,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
#ifndefANDROID_MISOO_SQR_H
#defineANDROID_MISOO_SQR_H
namespaceandroid{
classSQR{
private:
constvoidgetSQRService();
public:
SQR();
intexecute(intn);
};
};//namespace
#endif//ANDROID_MISOO_SQR_H
//sqr.cpp
#include<utils/IServiceManager.h>
#include<utils/IPCThreadState.h>
#include"SQR.h"
namespaceandroid{
sp<IBinder>m_ib;
SQR::SQR(){
getSQRService();
}
constvoidSQR::getSQRService(){
sp<IServiceManager>sm=defaultServiceManager();
m_ib=sm->getService(String16("misoo.sqr"));
LOGE("SQR:getSQRService%p\n",sm.get());
if(m_ib==0)
LOGW("SQRServicenotpublished,waiting...");
return;
}
intSQR::execute(intn){
Parceldata,reply;
data.writeInt32(n);
LOGE("SQR::execute\n");
m_ib->transact(0,data,&reply);
intnum=reply.readInt32();
returnnum;
}
};
============================================================
/*com_misoo_service_sqr01.h*/
/*DONOTEDITTHISFILE-itismachinegenerated*/
#include<jni.h>
/*Headerforclasscom_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
*/
JNIEXPORTjintJNICALLJava_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"
usingnamespaceandroid;
JNIEXPORTjintJNICALLJava_com_misoo_service_sqr01_execute(JNIEnv
*env,
jobjectthiz,jintx)
{
chars[]="Java_com_misoo_service_sqr01_execute!!!";
LOGE("%sX=%d\n",s,x);
SQR*sqrObj=newSQR();
intnum=sqrObj->execute(x);
returnnum;
}
//Android.mk
LOCAL_PATH:=$(callmy-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
packagecom.misoo.service;
publicclasssqr01
{
static
{System.loadLibrary("libSQRS01_jni");}
publicstaticnativeintexecute(intx);
}
//SuperSQR.java
packagecom.misoo.service;
abstractpublicclassSuperSQR{
publicintexec(){
returnsqr01.execute(onGetX());
}
abstractpublicintonGetX();
}
//mySQR.java
packagecom.misoo.pk01;
importcom.misoo.service.SuperSQR;
publicclassmySQRextendsSuperSQR{
@Override
publicintonGetX(){
return60;
}
}
//ac01.java
packagecom.misoo.pk01;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
publicclassac01extendsActivityimplementsOnClickListener{
privateButtonbtn,btn2;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
LinearLayoutlayout=newLinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn=newButton(this);btn.setBackgroundResource
(R.drawable.heart);
btn.setId(101);btn.setText("run");
btn.setOnClickListener(this);
LinearLayout.LayoutParamsparam=
newLinearLayout.LayoutParams(120,55);
param.topMargin=10;
layout.addView(btn,param);
btn2=newButton(this);btn2.setBackgroundResource
(R.drawable.ok);
btn2.setId(102);btn2.setText("exit");
btn2.setOnClickListener(this);
layout.addView(btn2,param);
setContentView(layout);
}
publicvoidonClick(Viewv){
switch(v.getId()){
case101:
mySQRsqr=newmySQR();
intk=sqr.exec();
setTitle("Value="+String.valueOf(k));
break;
case102:
finish();
break;
}
}}