一、在frameworks/base/core/java/android/os/ 增加一个aidl文件,最后用aidl工具生产一个Java文件,这样可以方便后面:然后在修改/frameworks/base/Android.mk,在LOCAL_SRC_FILES里添加一行:core/java/android/os/IYtTtsService.aidl \不确定补充:把AIDL文件写在这个地方似乎可以避免加@hide
二 、在frameworks/base/services/java/com/android/server/ 新建要增加的service,继承上面aidl生产的java
三、在frameworks/base/services/java/com/android/server/SystemServer.java, ServerThread中run()方法中增加代码
四、在ContextImpl getSystemService中添加代码
五、测试代码
/system/bin/screencap u:object_r:screencap_exec:s0
type screencap, domain,coredomain
type screencap_exec, exec_type,vendor_file_type,file_type
init_daemon_domain(screencap)
SystemProperties.set("ctl.start*", "xxx"),即可
1.添加.AIDL文件/frameworks/base/core/java/android/os/IHelloService.aidl
[java] view plain copy
package android.os
interface IHelloService
{
String test_service(String input)
}
在/frameworks/base/Android.mk 把aidl文件加入到源码编译器中
[java] view plain copy
core/java/android/os/IHelloService.aidl \
2.编写服务类
/frameworks/base/services/java/com/android/server/HelloService.java
[java] view plain copy
package com.android.server
import android.os.IHelloService
import android.os.IBinder
import android.content.Context
import android.util.Log
public class HelloService extends IHelloService.Stub
{
private static final String TAG="HelloService"
@Override
public String test_service(String input){
Log.i(TAG,"eva HelloService HelloService,test_service()) method")
return input
}
public HelloService()
{
super()
Log.i(TAG,"eva HelloService Constructor method")
}
}
3.编写服务管理器
/frameworks/base/core/java/android/os/TestServiceManager/frameworks/base/core/java/android/os/HelloManager.java
[java] view plain copy
package android.os
import android.os.IHelloService
import android.content.Context
import android.os.RemoteException
import android.util.Log
public class HelloManager
{
//Basic Member
android.os.IHelloService mService
private static final String TAG="HelloManager"
//Constructor
public HelloManager(Context ctx,android.os.IHelloService service)
{
mService=service
Log.i(TAG,"eva HelloManager Constructor method")
}
public String test_service(String input){
try{
Log.i(TAG,"eva HelloManager Constructor mService.test_service")
return mService.test_service(input)
}catch(RemoteException e)
{
return e.getMessage()
}
}
}
4.注册服务
/frameworks/base/core/java/android/app/SystemServiceRegistry.java
[java] view plain copy
import android.os.IHelloService
import android.os.HelloManager
registerService("HELLO_SERVICE",HelloManager.class,
new CachedServiceFetcher<HelloManager>(){
@Override
public HelloManager createService(ContextImpl ctx)
{
IBinder b = ServiceManager.getService("HELLO_SERVICE")
Log.i(TAG,"eva SystemServiceRegistry registerService method")
return new HelloManager(ctx,IHelloService.Stub.asInterface(b))
}})
5.启动服务
/frameworks/base/services/java/com/android/server/SystemServer.java
[java] view plain copy
import com.android.server.HelloService
ServiceManager.addService("HELLO_SERVICE", new HelloService())
6.将服务加入到源码中,编译备份
/external/sepolicy/service.te
[java] view plain copy
type hello_service, system_api_service, system_server_service, service_manager_type
7.给服务权限
/external/sepolicy/service_contexts
[java] view plain copy
HELLO_SERVICE u:object_r:hello_service:s0
8.Demo
MainActivity.java
[java] view plain copy
package com.example.testservicedemo
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.os.HelloManager
public class MainActivity extends Activity {
private EditText writeEdit
private Button readBtn
private TextView showInfo
private HelloManager mTestServiceManager
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
writeEdit = (EditText)findViewById(R.id.input)
readBtn = (Button)findViewById(R.id.read)
showInfo = (TextView)findViewById(R.id.showinfo)
mTestServiceManager = (HelloManager)getSystemService("HELLO_SERVICE")
readBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String inputString = writeEdit.getText().toString()
String result = mTestServiceManager.test_service(inputString)
showInfo.setText(result)
}
})
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)