其实Service类似于Activity,
继承Service要实现其中的一些方法
onBind(Intent intent)必须实现的一个方法,返回一个绑定的接口给Service
onCreate()当Service第一次创建由系统调用
onStart(Intent intent, int startId) 当通过startService()方法启动Service时,该方法调用
onDestory()
1、新建个显示的Activity
package com.ex;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button startbtn,stopbtn,bindbtn,unbindbtn;
private ServiceConnection conn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startbtn=(Button)findViewById(R.id.startButton);
stopbtn=(Button)findViewById(R.id.stopButton);
bindbtn=(Button)findViewById(R.id.bindButton);
unbindbtn=(Button)findViewById(R.id.unbindButton);
//启动Service
startbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.ex.MY_SERVICE");
startService(intent);
}
});
//停止Service
stopbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.ex.MY_SERVICE");
stopService(intent);
}
});
//链接对象
conn = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i("service","链接成功!");
Toast.makeText(MainActivity.this, "链接成功!", Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
Log.i("service","断开链接!");
Toast.makeText(MainActivity.this, "断开链接!", Toast.LENGTH_LONG).show();
}
};
//绑定Service
bindbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.ex.action.MY_SERVICE");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
//解除绑定
unbindbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.ex.action.MY_SERVICE");
unbindService(conn);
}
});
}
}
2、main.xml的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/startButton"
android:text="启动Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
<Button
android:id="@+id/stopButton"
android:text="停止Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
<Button
android:id="@+id/bindButton"
android:text="绑定Service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
<Button
android:id="@+id/unbindButton"
android:text="解除绑定"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
</LinearLayout>
3、创建一个Service,并覆盖其生命周期中的方法,并在各个方法中显示信息
package com.ex;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
@Override
//可以返回null,通常返回一个aidl定义的接口
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i("SERVICE", "onBind.......");
Toast.makeText(MyService.this, "onBind.....", Toast.LENGTH_LONG).show();
return null;
}
//service创建时期 调用
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("SERVICE", "onCreate.......");
Toast.makeText(MyService.this, "onCreate.....", Toast.LENGTH_LONG).show();
}
//当客户端调用startService()方法,启动Service时,该方法被调用
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Log.i("SERVICE", "onStart.......");
Toast.makeText(MyService.this, "onStart.....", Toast.LENGTH_LONG).show();
}
//当Service不再使用时调用
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("SERVICE", "onDestory.......");
Toast.makeText(MyService.this, "onDestory.....", Toast.LENGTH_LONG).show();
}
}
4、要在AndroidManifest.xml里面进行注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ex"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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="MyService" >
<intent-filter >
<action android:name="com.ex.MY_SERVICE"/>
</intent-filter>
</service>
</application>
</manifest>
说明:绑定Service需要三个参数,bindService(intent,conn,Service.BIND_AUTO_CREATE)
conn是ServiceConnection对象,创建这个对象要实现onServiceConnected()和onServiceDisconnected()两个方法来判断是否连接成功。第三个参数是如何创建Service,一般是指定绑定时自动创建