編輯:初級開發
1.2.2 範例程式(1):採單純繼承方法
1.2.2.1 撰寫步驟:
Step-1: 建立android專案:Px01。
Step-2: 撰寫Activity的子類別:ac01,其程式碼如下:
/* ac01.Java */
package com.misoo.pkzz;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextVIEw;
public class ac01 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private MediaPlayer mPlayer;
private myButton btn, btn2, btn3;
public TextVIEw tv;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrIEntation(LinearLayout.VERTICAL);
btn = new myButton(this);
btn.setId(101);
btn.setText("play");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(btn.get_width(),
btn.get_height());
param.topMargin = 10;
layout.addVIEw(btn, param);
btn2 = new myButton(this);
btn2.setId(102);
btn2.setText("stop");
btn2.setOnClickListener(this);
layout.addVIEw(btn2, param);
btn3 = new myButton(this);
btn3.setId(103);
btn3.setText("exit");
btn3.setOnClickListener(this);
layout.addVIEw(btn3, param);
tv = new TextVIEw(this);
tv.setTextColor(Color.WHITE);
tv.setText("Ready");
LinearLayout.LayoutParams param2 =
new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addVIEw(tv, param2);
setContentVIEw(layout);
//---------------------------------------
myMediaPlayer my_player = new myMediaPlayer();
mPlayer = my_player.create(this, R.raw.test_cbr);
}
public void onClick(VIEw v) {
switch(v.getId()){
case 101:
mPlayer.start();
break;
case 102:
mPlayer.stop();
break;
case 103:
finish();
break;
}
}
}
Step-3: 撰寫myMediaPlayer類別,其程式碼如下:
/* myMediaPlayer.Java */
package com.misoo.pkzz;
import android.content.Context;
import android.media.MediaPlayer;
public class myMediaPlayer extends MediaPlayer {
public static MediaPlayer create(Context context){
return MediaPlayer.create(context, R.raw.test_cbr);
}
}
Step-4: 撰寫Button的子類別:myButton,其程式碼如下:
/* myButton.Java */
package com.misoo.pkcc;
import android.content.Context;
import android.widget.Button;
public class myButton extends Button {
public myButton(Context ctx){
super(ctx);
super.setBackgroundResource(R.drawable.heart);
}
public int get_width(){
return 80;
}
public int get_height(){
return 50;
}
}
Step-5: 執行之。
1.2.2.2 說明:
在ac01類別的指令:
myMediaPlayer my_player = new myMediaPlayer();
mPlayer = my_player.create(this, R.raw.test_cbr);
所傳回來的是MediaPlayer類別之物件參考(即mPlayer的值),而不是myMediaPlayer類別的物件參考。既然是MediaPlayer類別之物件參考,則外界的類別或函數(如ac01類別)就能透過mPlayer參考(即使用MediaPlayer介面)來使用此新誕生的MediaPlayer類別之物件了,如下圖:
雖然在my_player物件裡也有一個MediaPlayer的小物件,但是mPlayer並不是參考到它,而是參考到由create()函數所誕生的心物件。
1.2.3 範例程式(2):採繼承與委託混合型
1.2.3.1 撰寫步驟:
Step-1: 建立android專案:Px02。
Step-2: 撰寫Activity的子類別:ac01,其程式碼如下:
/* ac01.Java */
package com.misoo.pkzz;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextVIEw;
public class ac01 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private myMediaPlayer my_player;
private myButton btn, btn2, btn3;
public TextVIEw tv;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrIEntation(LinearLayout.VERTICAL);
btn = new myButton(this);
btn.setId(101);
btn.setText("play");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(btn.get_width(),
btn.get_height());
param.topMargin = 10;
layout.addVIEw(btn, param);
btn2 = new myButton(this);
btn2.setId(102);
btn2.setText("stop");
btn2.setOnClickListener(this);
layout.addVIEw(btn2, param);
btn3 = new myButton(this);
btn3.setId(103);
btn3.setText("exit");
btn3.setOnClickListener(this);
layout.addVIEw(btn3, param);
tv = new TextVIEw(this);
tv.setTextColor(Color.WHITE);
tv.setText("Ready");
LinearLayout.LayoutParams param2 =
new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addVIEw(tv, param2);
setContentVIEw(layout);
//---------------------------------------
my_player = myMediaPlayer.create(this);
}
public void onClick(VIEw v) {
switch(v.getId()){
case 101:
my_player.start();
break;
case 102:
my_player.stop();
break;
case 103:
finish();
break;
}
}
}
Step-3: 撰寫myMediaPlayer類別,其程式碼如下:
/* myMediaPlayer.Java */
package com.misoo.pkzz;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
public class myMediaPlayer extends MediaPlayer {
private static MediaPlayer mSuper;
private static Context ctx;
public static myMediaPlayer create(Context context){
ctx = context;
mSuper = MediaPlayer.create(context, R.raw.test_cbr);
return new myMediaPlayer();
}
public void start(){
try{
mSuper.start();
((ac01)ctx).tv.setText("Playing audio...");
((Activity)ctx).setTitle("MP3 Music");
} catch (Exception e) {
Log.e("StartPlay", "error: " + e.getMessage(), e);
}
}
public void stop(){
if (mSuper != null) {
((ac01)ctx).tv.setText("Stop");
mSuper.stop();
mSuper.release();
mSuper = null;
}
}
}
Step-4: 撰寫Button的子類別:myButton,其程式碼如下:
/* myButton.Java */
package com.misoo.pkcc;
import android.content.Context;
import android.widget.Button;
public class myButton extends Button {
public myButton(Context ctx){
super(ctx);
super.setBackgroundResource(R.drawable.heart);
}
public int get_width(){
return 80;
}
public int get_height(){
return 50;
}
}
Step-5: 執行之。
1.2.3.2 說明:
在ac01物件裡的my_player是參考到myMediaPlayer的物件,此物件裡mSuper再參考到MediaPlayer的物件,如下圖:
在ac01類別裡的指令:
my_player.start();
是呼叫到myMediaPlayer子類別的start()函數,然後才委託呼叫MediaPlayer的start()函數。此時,可看到「繼承的副作用」了,就是:myMediaPlayer繼承了眾多函數,並無法透過my_palyer去呼叫之,甚至可能是有害的。因此宜改用下一小節的單純委託方法。
前言關鍵字: DigitalClock date formatandroid的DigitalClock並沒有設置輸出格式的屬性或方法,但是可以通過繼承重寫來實現,見正文
本人嘗試,裡面提供得軟件在索愛x8上失敗了 提示說不能網絡共享 。看來自己編譯x8迫在眉睫啊 (有幸下載到了x8得源代碼。。。)--------------------
ps:對於軟件開發人員來說,保護代碼安全也是比較重要的因素之一,不過目前來說Google Android平台選擇了Java Dalvik VM的方式使其程序很容易破解和
android系統中采用PULL技術解析XML文檔比用SAX技術性能要好得多。建議android的開發者采用PULL技術解析XML文檔。跟SAX類似,只不過PULL技