import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import java.util.Random;
public class TestService extends Service {
public class MyBinder extends Binder {
public TestService getService() {
return TestService.this;
}
}
private MyBinder binder = new MyBinder();
public int getRandomNumber() {
return new Random().nextInt();
}
@Override
public void onCreate() {
Log.e("buder", "TestService 执行 onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("buder", "TestService 执行 onStartCommand");
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
Log.e("buder", "TestService 执行 onBind");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("buder", "TestService 执行 onUnbind");
sleepingOne();
return false;
}
@Override
public void onDestroy() {
Log.e("buder", "TestService 执行 onDestroy");
super.onDestroy();
}
private void sleepingOne(){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TestService service = null;
private boolean isBind = false;
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
isBind = true;
TestService.MyBinder myBinder = (TestService.MyBinder) binder;
service = myBinder.getService();
Log.i("buder", "ActivityA 执行 onServiceConnected");
int num = service.getRandomNumber();
sleepingOne();
Log.i("buder", "ActivityA 执行 getRandomNumber = " + num);
}
@Override
public void onServiceDisconnected(ComponentName name) {
isBind = false;
Log.i("buder", "ActivityA 执行 onServiceDisconnected");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("buder", "ActivityA 执行 onCreate");
findViewById(R.id.btnBindService).setOnClickListener(this);
findViewById(R.id.btnUnbindService).setOnClickListener(this);
findViewById(R.id.btnStartActivityB).setOnClickListener(this);
findViewById(R.id.btnFinish).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBindService) {
//单击了“bindService”按钮
/**
* 显示启动service
*/
// Intent intent = new Intent(this, TestService.class);
// intent.putExtra("from", "ActivityA");
Log.i("buder", "ActivityA 执行 bindService");
/**
* 隐式启动service
*/
Intent intent = new Intent();
intent.putExtra("from", "ActivityA");
ComponentName componentName = new ComponentName(getPackageName(), "com.example.testservices.TestService");
intent.setComponent(componentName);
// startService(intent);
bindService(intent, connection, BIND_AUTO_CREATE);
} else if (v.getId() == R.id.btnUnbindService) {
//单击了“unbindService”按钮
if (isBind) {
Log.i("buder", "ActivityA 执行 unbindService");
unbindService(connection);
}
} else if (v.getId() == R.id.btnStartActivityB) {
//单击了“start ActivityB”按钮
Intent intent = new Intent(this, ActivityB.class);
Log.i("buder", "ActivityA 启动 ActivityB");
startActivity(intent);
} else if (v.getId() == R.id.btnFinish) {
//单击了“Finish”按钮
Log.i("buder", "ActivityA 执行 finish");
this.finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("buder", "ActivityA 执行 onDestroy");
}
private void sleepingOne(){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
public class ActivityB extends Activity implements View.OnClickListener {
private TestService service = null;
private boolean isBind = false;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
isBind = true;
TestService.MyBinder myBinder = (TestService.MyBinder) binder;
service = myBinder.getService();
Log.i("buder", "ActivityB 执行 onServiceConnected");
int num = service.getRandomNumber();
Log.i("buder", "ActivityB 执行 getRandomNumber = " + num);
}
@Override
public void onServiceDisconnected(ComponentName name) {
isBind = false;
Log.i("buder", "ActivityB 执行 onServiceDisconnected");
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
findViewById(R.id.btnBindService).setOnClickListener(this);
findViewById(R.id.btnUnbindService).setOnClickListener(this);
findViewById(R.id.btnFinish).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBindService) {
//单击了“bindService”按钮
Intent intent = new Intent(this, TestService.class);
intent.putExtra("from", "ActivityB");
Log.i("buder", "ActivityB 执行 bindService");
bindService(intent, conn, BIND_AUTO_CREATE);
} else if (v.getId() == R.id.btnUnbindService) {
//单击了“unbindService”按钮
if (isBind) {
Log.i("buder", "ActivityB 执行 unbindService");
unbindService(conn);
}
} else if (v.getId() == R.id.btnFinish) {
//单击了“Finish”按钮
Log.i("buder", "ActivityB 执行 finish");
this.finish();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("buder", "ActivityB 执行 onDestroy");
}
}
1.MainActivity点击bind
2.MainActivity点击UNbind
1.MainActivity的bind
2.MainActivity的finish
1.MainActivity的bind
2.MainActivity的startActivityB
3.ActivityB的bind
4.ActivityB的unbind
5.ActivityB的finish
6.MainActivity的unbind
完整的项目地址:https://github.com/buder-cp/base_component_learn/tree/master/testservices
本博客参考:https://blog.csdn.net/iispring/article/details/48169339#quote