一个简单的service实例

来源:互联网 发布:贵学软件 编辑:程序博客网 时间:2024/05/13 18:56

1.服务的生命周期:

服务只会被创建一次调用oncreate , 多次开启服务, 只会去重复调用 onstartcommand , 并不会去新创建服务, 不会调用oncreate

服务也只会被关闭一次, 开启服务后 可以去关闭服务, 会调用 ondestroy方法 销毁服务

结论:使用开启服务的方式 去调用服务中的方法 是 走不通的 , 调用不了 服务中的方法 ...

需要使用绑定服务去调用 服务中的方法....

2.开启和绑定服务,生命周期的区别

绑定服务完整的生命周期:

   绑定服务: 调用 oncreate,onbind方法;调用服务中的方法-------

   解绑服务: 调用 onunbind方法, 调用 ondestroy方法 服务销毁

绑定服务了之后 , 直接去解绑服务, 然后再去调用 服务中的方法 ..

区别,绑定服务的时候, 如果 绑定服务的应用退出了, 那么  服务也会 被销毁 ,而开启服务时, 如果 开启服务的 应用退出了, 但是 这个被开启的服务 仍然是 在 后台 运行的, 是一个后台的服务进程.

3.注意在解绑服务的时候,需要关闭这个代理,否则有可能出现memory leak异常

   关闭代理:

// memory leak @Overridepublic void onServiceDisconnected(ComponentName name) {agent = null; }
解绑的时候:

//解绑  服务public void unbindService(View v){Intent intent = new Intent();intent.setClass(this, TestService.class);if(conn!=null){//解绑 服务 unbindService(conn);//这里 养成好习惯, 将 conn conn =null;}}

4.混合开启服务时候需要注意

       开启服务

       绑定服务

       调用服务的方法

       解绑服务

      关闭服务

     如果以后需要去在后台一直运行服务, 并且又想调用服务中的方法, 那么请严格按照如上的 5 步去走.  否则可以能会导致一些莫名其妙的问题


另外附一个简单的demo:

mainactivity:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}MyConn conn;//绑定  服务public void bindService(View v){Intent intent = new Intent();intent.setClass(this, TestService.class);if(conn==null){conn =new MyConn();bindService(intent, conn, BIND_AUTO_CREATE);}}private IService agent;private class MyConn implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {System.out.println("=======服务 绑定的时候 执行了   onServiceConnected++++++");agent = (IService) service;}// memory leak @Overridepublic void onServiceDisconnected(ComponentName name) {agent = null; System.out.println("=======服务 绑定的时候 执行了   onServiceDisconnected");}}//解绑  服务public void unbindService(View v){Intent intent = new Intent();intent.setClass(this, TestService.class);if(conn!=null){//解绑 服务 unbindService(conn);//这里 养成好习惯, 将 conn conn =null;}}//调用服务 中的方法 public void callMethodInService(View v){agent.callMethodInService("二球", 1000);}}

testservice:

public class TestService extends Service {private class MyAgent extends Binder implements IService{@Overridepublic void callMethodInService(String name, int money) {//调用 服务中的方法methodInService(name, money);}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println(" onStartCommand  执行了  ");return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent intent) {System.out.println("on bind 执行了 , 绑定了 服务  ");return new MyAgent();}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("on onUnbind  执行了 ,  解绑 了 服务  ");return super.onUnbind(intent);}@Overridepublic void onCreate() {System.out.println(" 服务创建了 ");super.onCreate();}@Overridepublic void onDestroy() {System.out.println("服务销毁 了 ");super.onDestroy();}//服务中的方法 public void methodInService(String name, int money){Toast.makeText(this, "服务中的方法被调用到了", 0).show();System.out.println("服务中的方法被调用到了 ....");}}

IService接口

public interface IService {public void callMethodInService(String name,int money);}


0 0
原创粉丝点击