Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android開發教程之高煥堂-上課講義(7)

Android開發教程之高煥堂-上課講義(7)

編輯:初級開發

三、範例程式:Activity與Service間之溝通

先執行ac01:

clip_image054

這個ac01立即啟動myService,定時連續傳來數字,如下:

clip_image055

數字連續增加下去。其程式碼為:

/* aco1.Java */

package com.misoo.ppvv;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Color;

import android.os.Bundle;

import android.widget.TextVIEw;

public class ac01 extends Activity {

private TextVIEw tx;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentVIEw(R.layout.main);

tx = (TextView)findVIEwById(R.id.tx);

tx.setTextSize(26);

tx.setTextColor(Color.RED);

tx.setBackgroundResource(R.drawable.x_yellow);

//--------------------------------------

myService.setUpdateListener(new UpdateUIListener());

Intent svc = new Intent(this, myService.class);

startService(svc);

}

@Override protected void onDestroy() {

super.onDestroy(); {

Intent svc = new Intent(this, myService.class);

stopService(svc);

}

}

class UpdateUIListener implements IListener {

public void update(String s) {

tx.setText(" " + s);

}

}

}

/* myService.Java */

public class myService extends Service {

private static IListener plis;

private static int k = 0;

private Timer timer = new Timer();

public Handler handler = new Handler(){

public void handleMessage(Message msg) {

plis.update(String.valueOf(k++));

}

};

@Override

public void onCreate() {

super.onCreate();

TimerTask task = new TimerTask() {

@Override

public void run() {

handler.sendEmptyMessage(0);

}

};

timer.schedule(task,1000, 3000);

}

@Override

public IBinder onBind(Intent intent) {

return null;

}

public static void setUpdateListener(IListener listener) {

plis = listener;

}

}

四、結語

以上的Listener方法只適合於在同一個進程裡的物件之溝通;並不適合於跨進程(IPC)之溝通。

跨進程之溝通,可使用Intent、Binder等方式溝通。

=== END ===

講義摘錄之17: 如何使用SurfaceVIEw計時繪點或線
大家常撰寫VIEw的子類別,然後在其onDraw()函數裡繪出各式各樣的圖形,例如畫出點或是直線。不過,基本上onDraw()函數是在Canvas畫完所有線條等圖形後,才一塊兒將Canvas顯示出來。

然而,當我們想再畫出一些線條之後,停個幾秒鐘之後,才繼續繪出後續的圖形,這種有停頓的計時性繪圖,又該如何呢?

例如,先繪出一條綠色線段:

clip_image056

停頓5秒鐘之後,才畫出一條黃色線段:

clip_image057

這時,使用SurfaceVIEw會是一個好方法。如下之範例程式碼:

/* ac01.Java */

package com.misoo.ppvv;

import android.app.Activity;

import android.os.Bundle;

import android.widget.LinearLayout;

public class ac01 extends Activity {

@Override

protected void onCreate(Bundle icicle) {

super.onCreate(icicle);

MySurfaceVIEw mv = null;

try {

mv = new MySurfaceVIEw(this);

} catch (InterruptedException e) {

e.printStackTrace();

}

//----------------------------------

LinearLayout layout = new LinearLayout(this);

layout.setOrIEntation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams param =

new LinearLayout.LayoutParams(200, 150);

param.topMargin = 5;

layout.addVIEw(mv, param);

//----------------------------------------

setContentVIEw(layout);

}

}

/* MySurfaceVIEw.Java */

package com.misoo.ppvv;

import android.content.Context;

import android.graphics.Color;

import android.vIEw.SurfaceHolder;

import android.view.SurfaceVIEw;

class MySurfaceView extends SurfaceVIEw implements SurfaceHolder.Callback {

SurfaceHolder mHolder;

private DrawThread mThread;

private dwList dwl;

MySurfaceVIEw(Context context) throws InterruptedException {

super(context);

getHolder().addCallback(this);

dwl = new dwList();

dwl.begin_record();

dwl.record(30, 30, 0, 0);

dwl.record(100, 100, 1, Color.GREEN);

Thread.sleep(50);

dwl.record(100, 30, 1, Color.YELLOW);

Thread.sleep(60);

dwl.record(30, 30, 1, Color.BLUE);

Thread.sleep(40);

dwl.record(30, 100, 1, Color.RED);

}

public void surfaceCreated(SurfaceHolder holder) {

mHolder = holder;

mThread = new DrawThread();

mThread.start();

}

public void surfaceDestroyed(SurfaceHolder holder) {

mThread = null;

}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

}

// ----------------------------------------------------------------------

class DrawThread extends Thread {

DrawThread() {

super();

}

@Override

public void run() {

dwl.draw(mHolder);

}

}

}

/* dwList.Java */

package com.misoo.ppvv;

import Java.util.ArrayList;

import Java.util.Iterator;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.vIEw.SurfaceHolder;

public class dwList {

private Paint paint= null;

private long draw_time;

private ArrayList poList;

public void begin_record()

{

dwPoint.initial_time = System.currentTimeMillis();

poList.clear();

}

public void record(int x, int y, int ty, int color)

{ dwPoint po = new dwPoint(x, y, ty, color);

poList.add(po);

}

public dwList(){

paint = new Paint();

poList = new ArrayList();

}

public void draw(SurfaceHolder holder) {

dwPoint po;

long curr_time;

long base_time =0;

int nnn = 0;

Iterator it = poList.iterator();

while (it.hasNext() ){

po = it.next();

draw_time = po.m_timeSpan * 100;

if(nnn == 0)

base_time = System.currentTimeMillis();

nnn++;

//--------- waiting ----------------------------------

do {

curr_time = System.currentTimeMillis() - base_time;

}

while (curr_time < draw_time);

//------------------------------------------------------

paint(holder, nnn );

}

}

public void paint(SurfaceHolder holder, int k) {

Canvas canvas = holder.lockCanvas();

dwPoint po;

int lastX = 0;

int lastY = 0;

Iterator it = poList.iterator();

for (int i = 0; i

po = it.next();

if(po.m_type == 0 ) {

lastX = po.m_x;

lastY = po.m_y;

}

else {

paint.setColor(po.m_color);

paint.setStrokeWidth(3);

canvas.drawLine(lastX, lastY, po.m_x, po.m_y, paint);

lastX = po.m_x;

lastY = po.m_y;

}

}

holder.unlockCanvasAndPost(canvas);

}

}

/* dwPoint.Java */

package com.misoo.ppvv;

public class dwPoint {

public static long initial_time;

public int m_x, m_y, m_type;

public long m_timeSpan;

public int m_color;

public dwPoint() {}

public dwPoint(int x, int y, int ty, int cc) {

m_x = x;

m_y = y;

m_type = ty;

m_color = cc;

m_timeSpan = (long)(System.currentTimeMillis() - initial_time);

}

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved