以下是一个示例代码,演示了如何通过继承Service在后台运行,并每8ms更新坐标数据,并通知Activity以线的方式将坐标数据画出来。
MyService.java:
import android.app.Service;
import android.content.Intent;
import android.graphics.Point;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
public class MyService extends Service {
private final IBinder binder = new MyBinder();
private boolean isRunning;
private Timer timer;
private DrawViewCallback callback;
// 坐标数据
private int xCoord = 0;
private int yCoord = 0;
public interface DrawViewCallback {
void updateCoordinates(int x, int y);
}
public class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
isRunning = true;
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// 更新坐标数据
updateCoordinates();
}
}, 0, 8);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
isRunning = false;
if (timer != null) {
timer.cancel();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public void setCallback(DrawViewCallback callback) {
this.callback = callback;
}
// 更新坐标数据
private void updateCoordinates() {
// 模拟坐标更新
xCoord++;
yCoord++;
// 通知Activity更新坐标数据
if (callback != null) {
callback.updateCoordinates(xCoord, yCoord);
}
}
}
MainActivity.java:
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements MyService.DrawViewCallback {
private DrawView drawView;
private MyService myService;
private boolean isServiceBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = findViewById(R.id.draw_view);
drawView.setStrokeWidth(5f);
drawView.setStrokeColor(Color.RED);
}
@Override
protected void onStart() {
super.onStart();
bindService();
}
@Override
protected void onStop() {
super.onStop();
unbindService();
}
private void bindService() {
Intent intent = new Intent(this, MyService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
private void unbindService() {
if (isServiceBound) {
unbindService(serviceConnection);
isServiceBound = false;
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
MyService.MyBinder binder = (MyService.MyBinder) iBinder;
myService = binder.getService();
myService.setCallback(MainActivity.this);
isServiceBound = true;
System.out.println("Service bound.");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
myService = null;
isServiceBound = false;
System.out.println("Service unbound.");
}
};
@Override
public void updateCoordinates(final int x, final int y) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// 在DrawView上绘制线
drawView.drawLine(x - 1, y - 1, x, y);
}
});
}
}
在这个示例中,MyService继承自Service类,每8ms更新一次坐标数据,并通过DrawViewCallback接口通知MainActivity。MainActivity实现了DrawViewCallback接口,在回调方法中将新的坐标数据传递给DrawView,并在DrawView上绘制线。
在MainActivity中,通过bindService()方法将MainActivity与MyService进行绑定,并设置DrawView的线条宽度和颜色。在onStart()方法中进行绑定,onStop()方法中解除绑定。
在updateCoordinates()方法中,通过调用drawView的drawLine()方法,在DrawView上绘制线。
请确保在AndroidManifest.xml文件中声明MyService类:
<service
android:name=".MyService"
android:enabled="true"
android:exported="false" />
这样,MyService将作为一个后台服务运行,并且通过DrawViewCallback接口通知MainActivity更新坐标数据并在DrawView上绘制线。