首页 > 系统 > Android > 正文

给android源码加个服务Service

2019-11-08 00:06:32
字体:
来源:转载
供稿:网友
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:s08.DemoMainActivity.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);			}		});	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表