如何添加自己的系统服务于Android的框架? [英] How to Add our own System Service in Android Framework?

查看:112
本文介绍了如何添加自己的系统服务于Android的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和我已经分析了Android源$ C ​​$ C,了解系统服务是如何实现的。我的问题是我能够创建自己的系统服务,并将其添加到框架,使所有的应用程序应该能够访问我的服务。
任何意见/ code段将是有益的。

感谢留言者提前。


解决方案

 公共类为myService延伸服务{    私有静态实例为myService = NULL;    公共静态布尔isInstanceCreated(){
        例如回报!= NULL;
    }
    @覆盖
    公众的IBinder onBind(意向为arg0){
        // TODO自动生成方法存根
        返回null;
    }
    @覆盖
    公共无效调用onStart(意向意图,诠释startId){
        super.onStart(意向,startId);
        //Toast.makeText(this,调用onStart,Toast.LENGTH_SHORT).show();
        }
    @覆盖
    公共无效的onCreate(){
        super.onCreate();
        例如=这一点;
        新的Thread(threadBody)。开始();
    }
    @覆盖
    公共无效的onDestroy(){
        例如= NULL;
    }
}

在您的活动:

 如果(!myService.isInstanceCreated())
        startService(新意图(YourActivityClassName.this,myService.class));

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.

解决方案

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)); 

这篇关于如何添加自己的系统服务于Android的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆