How to Add our own System Service in Android Framework?

谁说胖子不能爱 提交于 2019-12-20 18:08:05

问题


I am new to android and i have been analyzing the android source code to understand how the System services are implemented. My Question is am I able to create my own System Service and add it to the framework so that all the applications should be Able to access my service. Any comments/Code snippets would be helpful.

Thanks to the Replier in advance.


回答1:


public class MyService extends Service {

    private static MyService instance = null;

    public static boolean isInstanceCreated(){
        return instance != null;
    }
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override     
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId); 
        //Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
        } 
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        new Thread(threadBody).start();
    }
    @Override     
    public void onDestroy() {
        instance = null;
    }
}

In your activity:

if(!MyService.isInstanceCreated())
        startService(new Intent(YourActivityClassName.this, MyService.class)); 



回答2:


For a system service, more is needed. Essentially, at least for Android 2.3, the SystemServer.java must be extended so that the new service gets instantiated when the system_server starts.

Texas Instruments has kindly provided a nice example:

http://processors.wiki.ti.com/index.php/Android-Adding_SystemService

The CyanogenMod SystemServer.java has also code for dynamically loading system services as defined in the array "config_vendorServices" in config.xml (see core/res/res/values/config.xml), which I believe can be overwritten in the vendor or device directories. However, we haven't tried to use that ourselves, and I don't know if that is CyanogenMod specific or common to 2.3.[45].




回答3:


You can add your own service but there is no way to add system service, unless you can modify the framework source code.

System services are running in system_sever with system privilege, which allow those service can access system resource that normal application service can't.

This article explain in detail how the system service are implemented in Android.




回答4:


Follow the below steps for writing your own System Service in android framework.

  1. Write your Own Service/Manager with API's exposed by inheriting the stub.
  2. Create an aidl file for your service to expose & include in build.
  3. Add your service in System Server, you service will start along with all core services.
  4. Register your service context in context impl file.
  5. Use your service in application by calling getSystemService(Context of your service)

PS: if your service get some fatal exception, device will soft reboot, as your service is running under system service.



来源:https://stackoverflow.com/questions/6649810/how-to-add-our-own-system-service-in-android-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!