一.使用Service的两种方式
1. startService与stopService
2. bindService与unbindService
两种方式的生命周期如下:
方式一:onCreate()----onStartCommand()---onDestroy()
需要注意的地方:
a. startService可以多次调用,不过onCreate方法只首次执行,onStartCommand()方法每调用一次startService,就执行一次。
b. stopService可以多次调用,不过onDestroy方法只首次执行。
c. 使用该方式,与Service通信可以借助onStartConmmand方式,在intent中传递参数。
d. 该方式只要启动,不调用stopService,当前调用者无论如何,Service都不会主动停止,这点和绑定服务(bindService)不同。
方式二:onCreate()---onBind()----onServiceConnected()---onUnBind()---onDestroy()
需要注意的地方:
a. bindService可以调用多次,不过onCreate,onBind,onServiceConnected都执行一次。
b. unbindService最多可以调用一次。
c. 使用该方式与Service通信,可以借助onBind返回接口的回调。
d. 该方式一般是和调用者绑定,带来的结果就是,当调用者销毁时,Service也会销毁。
一.简单使用
1. Activity代码如下:
package com.example.serviceapp;
import com.example.serviceapp.MyService.MyBinder;
import android.support.v7.app.ActionBarActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener{
private Intent mServiceIntent;
private Button mStartServiceBtn;
private Button mStopServiceBtn;
private Button mBindServiceBtn;
private Button mUnbindServiceBtn;
private EditText mEidtTextTx;
private Button mSendMsgBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mServiceIntent = new Intent(MainActivity.this, MyService.class);
initView();
initListeners();
}
private void initView() {
mStartServiceBtn = (Button) findViewById(R.id.start_service_btn);
mStopServiceBtn = (Button) findViewById(R.id.stop_service_btn);
mBindServiceBtn = (Button) findViewById(R.id.bind_service_btn);
mUnbindServiceBtn = (Button) findViewById(R.id.unbind_service_btn);
mEidtTextTx = (EditText) findViewById(R.id.edit_tx);
mSendMsgBtn = (Button) findViewById(R.id.send_msg_btn);
}
private void initListeners() {
mStartServiceBtn.setOnClickListener(this);
mStopServiceBtn.setOnClickListener(this);
mBindServiceBtn.setOnClickListener(this);
mUnbindServiceBtn.setOnClickListener(this);
mEidtTextTx.setOnClickListener(this);
mSendMsgBtn.setOnClickListener(this);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
int clickId = v.getId();
switch (clickId) {
case R.id.start_service_btn :
startService(mServiceIntent);
break;
case R.id.stop_service_btn :
stopService(mServiceIntent);
break;
case R.id.bind_service_btn :
bindService(mServiceIntent, serviceConn, Context.BIND_AUTO_CREATE);
break;
case R.id.unbind_service_btn :
unbindService(serviceConn);
break;
case R.id.send_msg_btn :
mBinder.sendMsg(mEidtTextTx.getText().toString());
break;
default :
break;
}
}
private ServiceConnection serviceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("onServiceDisconnected ------> + " + name);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("onServiceConnected ");
mBinder = (MyBinder) service;
}
};
private MyBinder mBinder;
}
2. Service代码如下:
package com.example.serviceapp;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service{
public class MyBinder extends Binder {
public void sendMsg(String msg) {
System.out.println("sendMsg --------> " + msg);
}
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind -------->");
return new MyBinder();
}
@Override
public void onCreate() {
System.out.println("onCreate -------->");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("onDestroy -------->");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand -------->");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onRebind(Intent intent) {
System.out.println("onRebind -------->");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("onUnbind -------->");
return super.onUnbind(intent);
}
}
3. XML文件代码如下:
<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: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.serviceapp.MainActivity"
android:orientation="vertical" >
<Button
android:id="@+id/start_service_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StartService" />
<Button
android:id="@+id/stop_service_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StopService" />
<Button
android:id="@+id/bind_service_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BindService" />
<Button
android:id="@+id/unbind_service_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UnbindService" />
<EditText
android:id="@+id/edit_tx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world" />
<Button
android:id="@+id/send_msg_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SendMsg" />
</LinearLayout>