1. 程式人生 > >給android原始碼加個服務Service

給android原始碼加個服務Service

1.新增.AIDL檔案

/frameworks/base/core/java/android/os/IHelloService.aidl

package android.os;

interface IHelloService
{
	String test_service(String input);
}

在/frameworks/base/Android.mk 把aidl檔案加入到原始碼編譯器中
core/java/android/os/IHelloService.aidl \

2.編寫服務類 /frameworks/base/services/java/com/android/server/HelloService.java
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
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
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
import com.android.server.HelloService;

ServiceManager.addService("HELLO_SERVICE", new HelloService());

6.將服務加入到原始碼中,編譯備份 /external/sepolicy/service.te
type hello_service, system_api_service, system_server_service, service_manager_type;

7.給服務許可權 /external/sepolicy/service_contexts
HELLO_SERVICE			u:object_r:hello_service:s0

8.Demo MainActivity.java
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);
			}
		});
	}
}