一。
startService:
生命周期为:
逻辑代码文件:
<span style="font-size:18px;">package com.example.day22_startservice_life;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
switch(v.getId())
{
case R.id.bt1:
Intent intent=new Intent(MainActivity.this, MyService.class);
startService(intent);
break;
case R.id.bt2:
Intent intent2=new Intent(MainActivity.this, MyService.class);
stopService(intent2);
break;
}
}
}
</span>
继承service的类文件:
<span style="font-size:18px;">package com.example.day22_startservice_life;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service{
String tag="##MyService##";
@Override
public IBinder onBind(Intent intent) {
Log.i(tag, "==onBind()==");
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(tag, "==onCreate()==");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(tag, "==onStartCommand()==");
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(tag, "==onDestroy()==");
}
}
</span>
布局文件:
<span style="font-size:18px;"><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/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动服务"
android:onClick="click"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="click"
android:layout_below="@id/bt1"/>
</RelativeLayout>
</span>
二。
bindService:
生命周期为:
----》
逻辑代码文件:
<span style="font-size:18px;">package com.example.day22_service3;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
public class MainActivity extends Activity {
MyServiceConn conn=new MyServiceConn();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
switch(v.getId())
{
case R.id.bt1:
Intent intent=new Intent(MainActivity.this,MyService.class);
/*
* 参数1:当前绑定服务的intent对象,指明绑定组件
* 参数2:当前服务的连接状态
* 参数3:绑定服务的一个标记,绑定并且启动
*/
bindService(intent, conn, Context.BIND_AUTO_CREATE);
break;
case R.id.bt2:
unbindService(conn);
break;
}
}
class MyServiceConn implements ServiceConnection
{
//服务连接成功后回调的方法
/*
* 参数1:组件的名称
* 参数2:连接后onBind()返回的值
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}
</span>
继承Service的类文件:
<span style="font-size:18px;">package com.example.day22_service3;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service{
String tag="##MyService##";
//通过bindService绑定服务,启动后回调的方法
@Override
public IBinder onBind(Intent intent) {
Log.i(tag, "==onBind==");
return null;
}
//服务创建时回调的方法
@Override
public void onCreate() {
super.onCreate();
Log.i(tag, "==onCreate==");
}
//通过unbindService解绑时回调的方法
@Override
public boolean onUnbind(Intent intent) {
Log.i(tag, "==onUnbind==");
return super.onUnbind(intent);
}
//有新的client连接到当前的服务
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.i(tag, "==onRebind==");
}
//服务销毁时回调的方法
@Override
public void onDestroy() {
super.onDestroy();
Log.i(tag, "==onDestroy==");
}
}
</span>
布局文件:
<span style="font-size:18px;"><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/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定服务"
android:onClick="click"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑服务"
android:onClick="click"
android:layout_below="@+id/bt1"/>
</RelativeLayout>
</span>