ServiceActivity.java
package cn.edu.zufe.app002;
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 androidx.appcompat.app.AppCompatActivity;
import cn.edu.zufe.app002.service.MyIntentService;
import cn.edu.zufe.app002.service.MyService;
public class ServiceActivity extends AppCompatActivity implements View.OnClickListener{
public static final String TAG = "app002.ServiceActivity";
private Button btnStart;
private Button btnStop;
private Button btnBind;
private Button btnUnbind;
private Button btnStartIs;
private ServiceConnection connection;
private MyService.DownloadBinder downloadBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
btnStart = (Button) findViewById(R.id.btn_start);
btnStop = (Button) findViewById(R.id.btn_stop);
btnBind = (Button) findViewById(R.id.btn_bind);
btnUnbind = (Button) findViewById(R.id.btn_unbind);
btnStartIs = (Button) findViewById(R.id.btn_start_is);
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (MyService.DownloadBinder) service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnBind.setOnClickListener(this);
btnUnbind.setOnClickListener(this);
btnStartIs.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
break;
case R.id.btn_stop:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent);
break;
case R.id.btn_bind:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.btn_unbind:
unbindService(connection);
case R.id.btn_start_is:
Log.d(TAG, "onClick: " + "Thead id is" + Thread.currentThread().getId());
Intent intentService = new Intent(this, MyIntentService.class);
startService(intentService);
default:
break;
}
}
}
activity_service.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ServiceActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_start"
android:text="启动服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_stop"
android:text="停止服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_bind"
android:text="绑定服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_unbind"
android:text="取消绑定" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_start_is"
android:text="启动IntentService" />
</LinearLayout>