您的位置:首页 > 移动开发 > Android开发

Android启动服务和绑定服务的方法调用

2017-06-01 23:26 537 查看
我们通过一个demo来学习启动服务和绑定服务的方法调用,以以下情况来观察Service中的方法调用:

首次启动Service

多次启动Service

首次绑定Service

多次绑定Service

启动Service后再绑定Service

绑定Service后再启动Service

贴上代码,布局文件,MainActivity,MyService

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">

<Button
android:id="@+id/start_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start service"
android:textAllCaps="false" />

<Button
android:id="@+id/stop_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop service"
android:textAllCaps="false" />

<Button
android:id="@+id/bind_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bind service"
android:textAllCaps="false" />

<Button
android:id="@+id/unbind_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="unbind service"
android:textAllCaps="false" />
</LinearLayout>


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.start_btn).setOnClickListener(this);
findViewById(R.id.stop_btn).setOnClickListener(this);
findViewById(R.id.bind_btn).setOnClickListener(this);
findViewById(R.id.unbind_btn).setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_btn:
startService();
break;
case R.id.stop_btn:
stopService();
break;
case R.id.bind_btn:
bindService();
break;
case R.id.unbind_btn:
unbindService();
break;
default:
break;
}
}

/**
* 启动服务
*/
private void startService() {
startService(new Intent(this, MyService.class));
}

/**
* 停止服务
*/
private void stopService() {
stopService(new Intent(this, MyService.class));
}

private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

}

@Override
public void onServiceDisconnected(ComponentName name) {

}
};

/**
* 绑定服务
*/
private void bindService() {
bindService(new Intent(this, MyService.cl
4000
ass), mServiceConnection, BIND_AUTO_CREATE);
}

/**
* 解绑服务
*/
private void unbindService() {
unbindService(mServiceConnection);
}
}


public class MyService extends Service {

private static final String TAG = "MyService";

public MyService() {
Log.e(TAG, "MyService()");
}

@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "MyService -> onCreate");
}

@Override
public int onStartCommand(Intent intent,int flags, int startId) {
Log.e(TAG, "MyService -> onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "MyService -> onBind");
return new MyBind();
}

@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "MyService -> onDestroy");
}

private static class MyBind extends Binder{

}
}


首次启动Service打印如下:



多次启动Service



首次绑定Service



多次绑定Service



启动Service后再绑定Service



绑定Service后再启动Service



到这里这几种情况就分析完了,有兴趣的同学可以试下哈,需要注意的是:

1.service要在AndroidManifest.xml文件中声明。

2.同时启动并且绑定的服务要执行停止服务和解绑服务的调用才会打印onDestroy方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐