您的位置:首页 > 移动开发 > Android开发

Android四大组件之一:Service(服务)

2016-03-24 10:55 666 查看
Service是Android中四大组件之一(Activity、Service、BroadcastReceiver、ContentProvider)

Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件。其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行。另外,一个组件能够绑定到一个service与之交互(IPC机制),例如,一个service可能会处理网络操作,播放音乐,操作文件I/O或者与内容提供者(content provider)交互,所有这些活动都是在后台进行

服务有两种启动方式:

一、采用start的方式开启服务 

其生命周期为:

   onStart()过时了

开启服务: onCreate()--> onStartCommand()  ---> onDestory();

如果服务已经开启,不会重复的执行onCreate(), 而是会调用onStart()和 onStartCommand();

服务停止的时候 onDestory().

服务只会被停止一次

二、绑定的方式开启服务。

其生命周期为:




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

绑定服务不会调用onstart或者onstartcommand方法;

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

 

start方式开启服务。 一旦服务开启跟调用者(开启者)就没有任何关系了。

开启者退出了,开启者挂了,服务还在后台长期的运行。

开启者没有办法去调用服务里面的方法。(美国的司法独立)

bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。不求同时生,但求同时死。

开启者可以调用服务里面的方法。



MainActivity.java

public class MainActivity extends Activity {

private MyConn conn;
private InterfaceMiddlePerson mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void startService(View v)  {
Intent intent = new Intent(this, MyService.class);
//MyConn conn;
conn = new MyConn();
boolean isSuccess = bindService(intent, conn, BIND_AUTO_CREATE);
if(isSuccess) {
Toast.makeText(getApplicationContext(), "服务被成功绑定了", 0).show();
}else {
Toast.makeText(getApplicationContext(), "服务没有成功绑定", 0).show();
}

//绑定服务代码

}
public void stopService(View v)  {
unbindService(conn);
}
public void callMethod(View v)  {
mp.callMethodInService(100);
}

private class MyConn implements ServiceConnection {

/*
* @Override
public IBinder onBind(Intent intent) {
return new MiddlePerson();
}
//此处得到IBinder service 建立链接后上面方法返回的MiddlePerson对象
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

Toast.makeText(getApplicationContext(), "得到中间人", 0).show();
mp = (InterfaceMiddlePerson) service;//得到中间人
}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

}

}
MyService.java

public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {
//Toast.makeText(getApplicationContext(), "服务被成功绑定了", 0).show();
return new MiddlePerson();
}

public void methodInService() {
Toast.makeText(getApplicationContext(), "我是服务里面的一个方法我被调用了", 0).show();
}

private class MiddlePerson extends Binder implements InterfaceMiddlePerson{//  Binder extends IBinder
public void callMethodInService(int money) {
if(money >= 50) {
methodInService();
}else{
Toast.makeText(getApplicationContext(), "我也无能为力啊。。。。", 0).show();
}

}
}

}
还有一个接口

public interface InterfaceMiddlePerson {
public void callMethodInService(int money);
}


还可以混合调用服务:

服务长期后台运行,又想调用服务的方法:

1.start方式开启服务(保证服务长期后台运行)

2.bind方式绑定服务(保证调用服务的方法)

3.unbind解除绑定服务

4.stopService停止服务。

<pre name="code" class="java">/*服务长期后台运行,又想调用服务的方法:
1.start方式开启服务(保证服务长期后台运行)
2.bind方式绑定服务(保证调用服务的方法)
3.unbind解除绑定服务
4.stopService停止服务。*/
Intent intent = new Intent(this, MusicPlayer.class);
startService(intent);
conn = new MyConn();
boolean isSuccess = bindService(intent, conn, BIND_AUTO_CREATE);
if(isSuccess) {
Toast.makeText(getApplicationContext(), "服务被成功绑定了", 0).show();
}else {
Toast.makeText(getApplicationContext(), "服务没有成功绑定", 0).show();
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: