一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

【轉(zhuǎn)】Android-Adding SystemService

 techres 2012-03-25


As per the definition given at http://developer./guide/topics/fundamentals/services.html

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

Service layer

Customservice.png

Create service

1. Add your code to frameworks/base/services/java/com/android/server/

/*TestService.java */
package com.android.server;
import android.content.Context;
import android.os.Handler;
import android.os.ITestService;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
public class TestService extends ITestService.Stub {
    private static final String TAG = "TestService";
    private TestWorkerThread mWorker;
    private TestWorkerHandler mHandler;
    private Context mContext;
    public TestService(Context context) {
		super();
		mContext = context;
		mWorker = new TestWorkerThread("TestServiceWorker");
		mWorker.start();
		Log.i(TAG, "Spawned worker thread");
   }
   
	public void setValue(int val) {
		Log.i(TAG, "setValue " + val);
		Message msg = Message.obtain();
		msg.what = TestWorkerHandler.MESSAGE_SET;
		msg.arg1 = val;
		mHandler.sendMessage(msg);
    }

	private class TestWorkerThread extends Thread{
		public TestWorkerThread(String name) {
    		super(name);
		}
		public void run() {
		    Looper.prepare();
		    mHandler = new TestWorkerHandler();
		    Looper.loop();
		}
	}
	
	private class TestWorkerHandler extends Handler {
		private static final int MESSAGE_SET = 0;
		@Override
		public void handleMessage(Message msg) {
		    try {
					if (msg.what == MESSAGE_SET) {
				    Log.i(TAG, "set message received: " + msg.arg1);
					}
	    		}catch (Exception e) {
					// Log, don't crash!
					Log.e(TAG, "Exception in TestWorkerHandler.handleMessage:", e);
    			}
		}
    }
}

Register service

  • Register service in SystemServer.java
/*
* go to function "@Override public void run()"
* ........ 
* Add following block after line "if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {"  
*/

try {
        Slog.i(TAG, "Test Service");
        ServiceManager.addService(“Test”, new TestService(context));
    } catch (Throwable e) {
        Slog.e(TAG, "Failure starting TestService Service", e);
    }

Expose service

  • A service can expose set of functions that can be access by other process/application. Exposed functions are required to be declared in .aidl file at following location

frameworks/base/core/java/android/os/[server].aidl

/*
* aidl file : frameworks/base/core/java/android/os/ITestService.aidl
* This file contains definitions of functions which are exposed by service 
*/
package android.os;
interface ITestService {
/**
* {@hide}
*/
	void setValue(int val);
}

Add [service].aidl for build

/*
* open frameworks/base/Android.mk and add following line
*/
...
core/java/android/os/IPowerManager.aidl core/java/android/os/ITestService.aidl core/java/android/os/IRemoteCallback.aidl ...
  • Rebuild the framework/base or android system.Service is now ready to use by other application/process.

Using service

To use service

  • first get service handle using "ServiceManager.getService()" api
  • use service handle to call set of functions exposed by service

Below is the sample code to use service.

/*
* HelloServer.java
*/
package com.Test.helloserver;
import android.app.Activity;
import android.os.Bundle;
import android.os.ServiceManager;
import android.os.ITestService;
import android.util.Log;
public class HelloServer extends Activity {
    private static final String DTAG = "HelloServer";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ITestService om = ITestService.Stub.asInterface(ServiceManager.getService("Test"));
        try {
			    Log.d(DTAG, "Going to call service");
			    om.setValue(20);
			    Log.d(DTAG, "Service called succesfully");
        }
        catch (Exception e) {
			    Log.d(DTAG, "FAILED to call service");
    			e.printStackTrace();
        }
    }
}


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多

    久久婷婷综合色拍亚洲| 国产一区二区三区丝袜不卡| 精品精品国产欧美在线| 麻豆最新出品国产精品| 老熟女露脸一二三四区| 少妇淫真视频一区二区| 好吊日成人免费视频公开| 国产原创中文av在线播放 | 亚洲中文字幕人妻系列| 亚洲午夜精品视频观看| 91福利免费一区二区三区| 成人免费在线视频大香蕉| 国产毛片不卡视频在线| 99久久人妻中文字幕| 女人高潮被爽到呻吟在线观看| 亚洲综合香蕉在线视频| 日木乱偷人妻中文字幕在线| 午夜福利网午夜福利网| 99一级特黄色性生活片| 国产精品欧美一区二区三区不卡| 在线懂色一区二区三区精品| 中文字日产幕码三区国产| 久久99精品国产麻豆婷婷洗澡| 国内午夜精品视频在线观看| 亚洲a级一区二区不卡| 国产一区二区三区香蕉av| 高清欧美大片免费在线观看| 在线懂色一区二区三区精品| 国产精品欧美一区二区三区| 98精品永久免费视频| 国产伦精品一区二区三区精品视频| 欧美av人人妻av人人爽蜜桃| 九九热精品视频免费在线播放| 亚洲最新中文字幕在线视频| 午夜传媒视频免费在线观看| 激情丁香激情五月婷婷| 很黄很污在线免费观看| 黄片免费在线观看日韩| 日本精品理论在线观看| 在线观看视频成人午夜| 又黄又硬又爽又色的视频|