new 一个thread
以下是MainActivity.java
package com.example.project0825;
import androidx.appcompat.app.AppCompatActivity;
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.view.View;
public class MainActivity extends AppCompatActivity {
BindService.MyBinder myBinder;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("--Service connected--");
myBinder = (BindService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("--Service Disconnected--");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onViewClick(View view) {
switch (view.getId()) {
case R.id.bind_service:
final Intent intent = new Intent(this, BindService.class);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
myBinder.isStopTHread();
unbindService(conn);
break;
default:
break;
}
}
}
以下是BindService.java
package com.example.project0825;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import androidx.annotation.Nullable;
public class BindService extends Service {
boolean isStop = false;
Thread mThread;
// 定义onBind方法返回的对象
private MyBinder myBinder = new MyBinder();
public class MyBinder extends Binder {
public void isStopTHread() {
isStop = true;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
System.out.println("Service is Binded");
return myBinder;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service is onCreate");
mThread = new Thread() {
@Override
public void run() {
super.run();
int i = 0;
while (!isStop) {
try {
System.out.println(i++);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
mThread.start();
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("Service is onUnbind");
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("Service is onDestroy");
}
}
附上布局文件activity_main.xml
<?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"
tools:context="com.example.project0825.MainActivity"
>
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onViewClick"
android:text="@string/bind_text"
/>
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onViewClick"
android:text="@string/unbind_text"
/>
</LinearLayout>