service是android的一个组件,相当于一个没有界面的activity,每个service都要在mainfest中进行注册,service有两种启动方式:Context.startService()和Context.bindService().
在详述这两种启动方式之前需要强调:
1.service不是一个单独的进程(可以看我的上一篇博客),如果在mainfest中不设置process属性,service会在默认进程里运行,
2.service和其他组件一样,运行在宿主进程的
主线程
里,service不是新的线程!!!!如果在service有耗时的工作,请另开thread,防止阻塞main thread。
有了上述的两个知识点,就可以进行servie的启动方式的学习了,由于篇幅较长,本文先讲第一种启动方式,第二种启动方式在下一篇博客中讲述。
service的第一种启动方式:
就是前面提到的Context.startService(),我们的activity,service都是一个Context(虚类),我们这里的例子以在activity中其中为例。
1 Service类的声明
和Activity一样,只不过service继承的是Service类,有几个函数可以override,我在这里声明的Servie如下:
public class MyService extends Service{
String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() executed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() executed");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() executed");
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
2 在mainfest.xml中注册
<service
android:name="com.example.servicetest.MyService">
</service>
3 启动
本文将用一个activity对service进行启动,需要声明一个Intent,在activity中有两个按钮,按下start后启动service,activity的代码如下:
public class MainActivity extends Activity {
Intent serviceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceIntent = new Intent(this , MyService.class);
Button startButton = (Button)findViewById(R.id.startbutton);
startButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//启动service
startService(serviceIntent);
}
});
Button stopButton = (Button)findViewById(R.id.stopbutton);
stopButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//停止service
stopService(serviceIntent);
}
});
}
@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;
}
}
布局文件如下:
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="@+id/startbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startService"/>
<Button
android:id="@+id/stopbutton"
android:layout_below="@id/startbutton"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stopService"/>
</RelativeLayout>
4 启动流程
在activity中,点击startbutton后输出如下:
在activity中,点击startbutton后输出如下:
点击stopbutton后输出如下:
这其实就是对service的一个完美诠释:
在activity里调用startService()之后在service里onCreate()->onStartCommand()
而在activity里调用stopService()之后在service里onDestroy(),即service终止
后记:用这种方法启动的service和actiivty没联系,但是在默认情况下是在同一进程的同一线程中的,如果此时终止activity,service还会继续运行,但是由于service在系统中的优先级较低,在内存不足时可能会被系统收回。