How to Add our own System Service in Android Framework?

前端 未结 4 915
抹茶落季
抹茶落季 2021-02-05 15:08

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 Se

4条回答
  •  清歌不尽
    2021-02-05 15:57

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

提交回复
热议问题