service:服务 是运行后台,他是没有界面的。对某件事情进行监听。服务他是不能自己运行,必须手动激活。
以下开始介绍启动服务的两种方式:
一、启动服务的两种方式
1、startService(intent)
生命周期:onCreate() ------>>onStart()-------->>onDestroy()
解释:
1) onCreate() 只会被调用一次 , onStart() 只要启动就会多次调用 ,onDestory() 只会被调用一次
2)startService()是会调用onCreate() ------>>onStart()。stopService()时会调用onDestroy()
3)用startService()的方式去启动服务,就必须使用stopService()的方式去停止服务
4)访问者与服务之间 没有关联关系。如Activity退出后,Service依然可以正常工作...
2、bindService(intent,conn,BIND_AUTO_CREATE)
生命周期:onCreate()--------->>onBind()----------->>onUnbind()----------->>onDestroy()
解释:
1)onCreate()只会被调用一次,onBind()只会被调用一次,onUnbind()只会被调用一次,onDestroy()只会被调用一次
2)bindService(intent,conn,BIND_AUTO_CREATE)绑定服务时,调用onCreate()--------->>onBind()。unBindService()解除绑定时会调用onUnbind()----------->>onDestroy()
3)一般情况下,使用bindService()去绑定服务,就只能使用unbindService()取解除绑定.可以多次绑定,但是解绑服务只能执行一次,多次解绑就会出错。
4)访问者与服务之间有关联关系。如Activity退出后(如果还没有解除绑定),Service不会继续工作了。
其过程为:抛出一个异常,然后自动执行unbindService()(unbindService()方法会自动去执行onUnbind() 方法);
抛出的异常如下:
Activity com.njupt.service.MainActivity has leaked ServiceConnection com.njupt.service.MainActivity$MyServiceConnection@43e76590 that was originally bound here
@Override
protected void onDestroy() {
super.onDestroy();
if(conn != null){
unbindService(conn);
}
}
**************需要注意的是,上面所提到的onCreate()、onStart()、onBind()、onUnbind()、onDestroy()都指的是Service组件中的方法,而不是Activity组件中的方法。这里要区分开来,别搞混了。。。。。
二、Activity中的几个方法与Service组件中的几个方法的联系
Activity Service
startService(intent) ---(调用)-----> onCreate() 、onStart()
stopService() ---(调用)-----> onDestroy()
bindService() ---(调用)-----> onCreate()、onBind()
unbindService() ---(调用)-----> onUnbind()、onDestroy()
代码如下:
1、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"
android:onClick="startservice"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="stopservice"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="bindservice"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑服务"
android:onClick="unbindservice"
/>
</LinearLayout>
2、MainActivity
package com.njupt.service;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private MyServiceConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startservice(View v){
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
public void stopservice(View v){
Intent intent = new Intent(this,MyService.class);
stopService(intent);
}
public void bindservice(View v){
Intent intent = new Intent(this,MyService.class);
conn = new MyServiceConnection();
bindService(intent, conn, BIND_AUTO_CREATE);
}
private class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void unbindservice(View v){
unbindService(conn);
conn = null;
}
@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;
}
@Override
protected void onDestroy() {
super.onDestroy();
if(conn != null){
unbindService(conn);
}
}
}
3、MyService
package com.njupt.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
System.out.println("onCreate调用");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
System.out.println("onStart调用");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("onDestroy调用");
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind调用");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("onUnbind调用");
return super.onUnbind(intent);
}
}
4、AndroidManfest.xml
注册Service组件
<service android:name="com.njupt.service.MyService"/>