Junior — Middle
What functions or methods can be implemented or modified when developing a custom View component?
sobes.tech AI
Answer from AI
In the context of Android development, the Service class can be extended to create a background service. To manage its behavior and rendering, you can override or implement the following methods:
onCreate()— called when the service is first created.onStartCommand(Intent intent, int flags, int startId)— called when the service is started.onBind(Intent intent)— used for bound services, to return anIBinderfor clients.onDestroy()— called when the service is being destroyed.onRebind(Intent intent)— called when new clients bind to the service.onUnbind(Intent intent)— called when all clients have unbound.onTaskRemoved(Intent rootIntent)— called when the service is killed due to task removal.
Example of overriding onDraw in a custom View for drawing a circle:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, 50, paint);
}
This allows you to control the visual representation, size, and interaction of a custom component.