Android源码添加服务

如何给源码添加一个服务呢。。放在java层,不要系统服务,就独立于系统进程为一个单独进程。。... 如何给源码添加一个服务呢。。放在java层,不要系统服务,就独立于系统进程为一个单独进程。。 展开
 我来答
匿名用户
2017-11-14
展开全部
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);
}
});
}
}
匿名用户
2017-11-14
展开全部
加在AndroidManifest.xml 文件中manifest标签之后以内,application以外
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式