码迷,mamicode.com
首页 > 其他好文 > 详细

Service

时间:2015-05-18 13:07:44      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:后台运行   优先级   start   而且   回收   

服务的目的:

1、     长期后台运行

2、     提高进程的优先级,系统不容易回收掉进程,即便回收了,内存充足的时候,把进程重新创建。

服务的生命周期:

开启服务:(调用服务里的方法,不可以自己new服务来调用服务里的方法,必须通过框架得到服务的引用,因为new只是得到一个普通的对象)

  • 采用start的方式开启服务

startService(intent)/stopService(intent)

onCreate()->OnStartCommand()->onDestory();

如果服务已经开启,不会重复执行onCreate(),而是会调用OnStartCommand()

服务停止时调用onDestory()

  • 绑定方式开启服务

onCreate()->onBind()->onunbind()->onDestory()

绑定服务不会调用OnStartCommand()方法

 

两种开启服务方法的区别:

start方式开启服务,一旦服务开启就跟调用者无关,即开启者挂了,但服务还在后台运行。而且,开启者不能调用服务里的方法

bind的方式开启服务,即绑定服务,调用者挂了,服务也会跟着挂掉。开启者可以调用服务里的方法

public class MainActivity extends Activity {

private MyConn conn;

private MiddlePerson middleperson;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

public void click_1(View view){

Intent intent_1 = new Intent(this,MyService.class);

startService(intent_1);

}

public void click_2(View view ){

Intent intent_2 = new Intent(this,MyService.class);

stopService(intent_2);

}

public void click_3(View view){

Intent intent = new Intent(this, MyService.class);

conn = new MyConn();

bindService(intent, conn, BIND_AUTO_CREATE);

}

public void click_4(View view){

unbindService(conn);

middleperson = null;

}

public void click_5(View view){

//通过绑定方法调用服务中的方法

middleperson.callMethodInService(60);

}

private class MyConn implements ServiceConnection{

        

//当服务连接时调用

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

// TODO Auto-generated method stub

System.out.println("得到服务的中间人");

middleperson = (MiddlePerson)service;

}

//当服务失去连接的时候调用(一般进程挂了,服务被异常杀死)

@Override

public void onServiceDisconnected(ComponentName name) {

// TODO Auto-generated method stub

}

}

}

public class MyService extends Service {

/**

* 绑定服务时调用

*/

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

System.out.println("服务被成功绑定");

return new MiddlePerson();

}

/**

* 销毁绑定时调用

*/

@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

return super.onUnbind(intent);

}

@Override

public void onCreate() {

System.out.println("oncreat");

super.onCreate();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

System.out.println("onStartCommand");

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

System.out.println("onDestroy");

super.onDestroy();

}

public void test(){

Toast.makeText(this, "调用了服务里的方法", 0).show();

}

/**

* 创建中间人,为绑定服务做准备

* @author lanxinlanxi

*

*/

public class MiddlePerson extends Binder{

public void callMethodInService(int mm){

if(mm>= 50){

test();//调用服务里的方法

}

else{

Toast.makeText(getApplicationContext(), "=====", 0).show();

}

}

}

}




本文出自 “小狼前行” 博客,谢绝转载!

Service

标签:后台运行   优先级   start   而且   回收   

原文地址:http://lanxi.blog.51cto.com/9610588/1652180

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!