myservice
package com.ttit.core.service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService2 extends Service {
private final String TAG = "service";
private int count;
private boolean quit;
//定义onBinder方法所返回的对象
private MyBinder binder = new MyBinder();
public class MyBinder extends Binder {
public int getCount() {
return count;
}
}
//必须实现的方法,绑定Service时回调该方法
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind方法被调用!");
return binder;
}
//Service被创建时回调
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate方法被调用!");
//创建一个线程动态地修改count的值
new Thread() {
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
Log.e(TAG, "count = " + count);
}
}
}.start();
}
//Service断开连接时回调
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG, "onUnbind方法被调用!");
return true;
}
//Service被关闭前回调
@Override
public void onDestroy() {
super.onDestroy();
this.quit = true;
Log.e(TAG, "onDestroyed方法被调用!");
}
@Override
public void onRebind(Intent intent) {
Log.e(TAG, "onRebind方法被调用!");
super.onRebind(intent);
}
}
Service2Activity
package com.ttit.core.service;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.ttit.helloworld.R;
public class Service2Activity extends AppCompatActivity {
private Button btnbind;
private Button btncancel;
private Button btnstatus;
//保持所启动的Service的IBinder对象,同时定义一个ServiceConnection对象
private MyService2.MyBinder binder;
//conn是为了监听客户端与服务端的链接情况
private ServiceConnection conn = new ServiceConnection() {
//Activity与Service断开连接时回调该方法
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("service", " ------Service DisConnected-------");
}
//Activity与Service连接成功时回调该方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("service", " ------Service Connected------ - ");
binder = (MyService2.MyBinder) service;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.service2_layout);
btnbind = (Button) findViewById(R.id.btnbind);
btncancel = (Button) findViewById(R.id.btncancel);
btnstatus = (Button) findViewById(R.id.btnstatus);
final Intent intent = new Intent(Service2Activity.this, MyService2.class);
//点击绑定按钮时链接
btnbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//绑定service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
btncancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//解除service绑定
unbindService(conn);
}
});
btnstatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Service的count的值为:"
+ binder.getCount(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn); // 销毁Activity时需要解绑Service
}
}