Service是Android系统的四大组件之一,是运行在后台的一种服务程序,一般生命周期较长,不直接与用户进行交互。
启动服务有两种方式:
1.startService()
2.bindService()
一、实现步骤
1.继承Service类,主要实现以下方法:
abstract IBinder onBind(Intent intent);//必须实现的返回一个绑定的接口给Service;
void onCreat()//当Service第一次启动时,调用该方法;
int onStartCommand(Intent intent,int flags,int startId) //当通过startService启动服务时调用该方法,并且每次调用startService都会调用该方法。
void onDestroy()//结束时调用
boolean onUnbind(Intent intent)//当通过bindService()方法启动service,取消绑定时调用。
下面介绍第一种方式:startService启动Service:上代码:
//布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testservice1.TestActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="StartServiceListener"
android:text="start" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="StopServiceListener"
android:text="stop" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="startActivity" />
</LinearLayout>
//主activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestActivity extends Activity {
private Button startbutton;
private Button stopbutton;
private Button startActivitybutton; //这个暂时不需要,看到可以不管
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
startbutton = (Button)findViewById(R.id.button1);
stopbutton = (Button)findViewById(R.id.button2);
// startActivitybutton = (Button) findViewById(R.id.button3);
System.out.println("Activity create");
}
public void StartServiceListener(View v){
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
startService(intent);
}
public void StopServiceListener(View view){
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
stopService(intent);
}
/* class StopServiceListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
stopService(intent);
}
} */
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
System.out.println("TestActivity->onStart");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
System.out.println("TestActivity->onRestart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("TestActivity->onRestart");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
System.out.println("TestActivity->onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
System.out.println("TestActivity->onStop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("TestActivity->onDestroy");
}
}
//service类:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class FirstService extends Service{
/**
* @param args
*/
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("Service onBind");
return null;
}
public void onCreate(){ //启动创建Service
super.onCreate();
System.out.println("Service onCreat");
}
//intent是startService时传过来的,每次调用startService都会调用该方法
public int onStartCommand(Intent intent,int flags,int startId){//启动service会调用这个方法
System.out.println("flags--->"+flags);
System.out.println("startId--->"+startId);
System.out.println("service onStartCommand");
return START_NOT_STICKY;
}
public void onDestroy(){
super.onDestroy();
System.out.println("service destroy");
}
}
//最后在Manifest.xml文件中别忘声明:
<service android:name=".FirstService" ></service>
2.至此一个Service已经启动完毕。
1) 设置Service继承Service类
2)启动service
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
startService(intent);
截图如下:
3.这只是简单的介绍了启动service,要想实现service和Activity通信,最好用第二种方式bindService启动service,可以获得service实例,当然就可以通信啦。
可以参照我前边写的service与activity通信的博客:http://blog.csdn.net/jycboy/article/details/46040189。