binding a service to multiple activities

吃可爱长大的小学妹 提交于 2019-12-05 11:39:04
JIthin

You have to use the same object of the Service call in both the activities. The best way to do it will be to extend the Application class where you have to write code to start service and to stop.Then you can access the service from any activity.

    public class AppController extends Application {

     private static AppController mInstance;
     private ExampleService service;
     public static synchronized AppController getInstance() {
            return mInstance;
     }

     @Override
     public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }
    public void startService(){
    //start your service

    }
    public void stopService(){
    //stop service
    }
    public ExampleService getService(){
    }
    }

Now to get the service from activity

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