Service(原文http://blog.csdn.net/android_tutor/article/details/5789203)
如果我们想保持和 Service 的通信,又不想让 Service 随着 Activity 退出而退出呢?你可以先 startService() 然后再 bindService() 。当你不需要绑定的时候就执行 unbindService() 方法,执行这个方法只会触发 Service 的 onUnbind() 而不会把这个 Service 销毁。这样就可以既保持和 Service 的通信,也不会随着 Activity 销毁而销毁了。
在Service的生命周期中,被回调的方法比Activity少一些,只有onCreate, onStart, onDestroy,onBind和onUnbind。
通常有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。
1 通过startService
Service会经历 onCreate 到onStart,然后处于运行状态,stopService的时候调用onDestroy方法。
如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。
2 通过bindService
Service会运行onCreate,然后是调用onBind, 这个时候调用者和Service绑定在一起。调用者退出了,Srevice就会调用onUnbind->onDestroyed方法。
所谓绑定在一起就共存亡了。调用者也可以通过调用unbindService方法来停止服务,这时候Srevice就会调用onUnbindonUnbind->onDestroyed方法。
需要注意的是如果这几个方法交织在一起的话,会出现什么情况呢?
一个原则是Service的onCreate的方法只会被调用一次,就是你无论多少次的startService又bindService,Service只被创建一次。
如果先是bind了,那么start的时候就直接运行Service的onStart方法,如果先是start,那么bind的时候就直接运行onBind方法。
如果service运行期间调用了bindService,这时候再调用stopService的话,service是不会调用onDestroy方法的,service就stop不掉了,只能先UnbindService, 再StopService。
如果一个service通过startService 被start之后,多次调用startService 的话,service会多次调用onStart方法。多次调用stopService的话,service只会调用一次onDestroyed方法。
如果一个service通过bindService被start之后,多次调用bindService的话,service只会调用一次onBind方法。多次调用unbindService的话会出异常。
第一步:新建一个Appication
第二步:修改xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/startservice"
android:text="startservice"/>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/stopservice"
android:text="stopservice"/>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/bindservice"
android:text="bindservice"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/unbindservice"
android:text="unbindservice"
/>
</LinearLayout>
第三步:新建一个Service,命名为MyService.java代码如下:
[java]
view plain
copy
- package com.android.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;public class MyService extends Service{
private static final String TAG=”MyService”;
private Mybinder mybinder=new Mybinder();
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG,”….onBind”);
return mybinder;
}
@Override
public void onCreate() {
Log.i(TAG,”….onCreate”);
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG,”….onDestroy”);
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,”….onStart”);
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,”….onUnBind”);
return super.onUnbind(intent);
}
public String getsystemTime(){
Time time=new Time();
time.setToNow();
return time.toString();
}
public class Mybinder extends Binder{
public MyService getService(){
return MyService.this;
}
}
}
第四步:修改MainActivity .java,代码如下:
package com.example.myservice;
import com.android.service.MyService;
import com.android.service.MyService.Mybinder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button startservice,stopservice,bindservice,unbindservice;
private MyService myService;
private Context mContext;
private static final String TAG="MainActivity";
private ServiceConnection serviceConnection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.i(TAG, "onServiceDisconnected.......");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i(TAG, "onServiceConnected.......");
myService=((Mybinder)service).getService();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=MainActivity.this;
startservice=(Button)findViewById(R.id.startservice);
stopservice=(Button)findViewById(R.id.stopservice);
bindservice=(Button)findViewById(R.id.bindservice);
unbindservice=(Button)findViewById(R.id.unbindservice);
startservice.setOnClickListener(this);
stopservice.setOnClickListener(this);
bindservice.setOnClickListener(this);
unbindservice.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
switch(v.getId()){
case R.id.startservice:
intent.setClass(MainActivity.this, MyService.class);
mContext.startService(intent);
break;
case R.id.stopservice:
intent.setClass(MainActivity.this, MyService.class);
mContext.stopService(intent);
break;
case R.id.bindservice:
intent.setClass(MainActivity.this, MyService.class);
mContext.bindService(intent, serviceConnection, BIND_AUTO_CREATE);
break;
case R.id.unbindservice:
intent.setClass(MainActivity.this, MyService.class);
mContext.unbindService(serviceConnection);
break;
}
}
}
第五步:修改AndroidManifest.xml代码
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myservice”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.myservice.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<service android:name=”com.android.service.MyService”
android:exported=”true”></service>
</application>
</manifest>
第六步:执行上述工程,效果图如下:
点击startServie按钮时先后执行了Service中onCreate()->onStart()这两个方法,打开Logcat视窗效果如下图:
我们这时可以按HOME键进入Settings(设置)->Applications(应用)->Running Services(正在运行的服务)看一下我们新启动了一个服务,效果如下:
点击stopService按钮时,Service则执行了onDestroy()方法,效果图如下所示:
这时候我们再次点击startService按钮,然后点击bindService按钮(通常bindService都是bind已经启动的Service),我们看一下Service执行了IBinder()方法,以及TextView的值也有所变化了,如下两张图所示:
最后点击unbindService按钮,则Service执行了onUnbind()方法,如下图所示:
播放音乐
package com.example.test1;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
MediaPlayer player = null;
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException(“Not yet implemented”);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this,
“MyService Createed!”,
Toast.LENGTH_SHORT).show();
player = MediaPlayer.create(this, R.raw.burning);
player.setLooping(false);
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this,
“MyService onStart!”,
Toast.LENGTH_SHORT).show();
player.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
Toast.makeText(this,
“MyService onDestroy”,
Toast.LENGTH_SHORT).show();
}
}