Android App的源代码:
---------------------------------------------------------------------------------------------
// myActivity.java
package com.misoo.pk01;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
publicclass myActivity extends Activity implements OnClickListener {
privatefinalintWC = LinearLayout.LayoutParams.WRAP_CONTENT;
privatefinalintFP = LinearLayout.LayoutParams.FILL_PARENT;
private Button btn, btn2, btn3;
publicTextView tv;
private IBinder ib = null;
privatefinal String MY_S_EVENT =
newString("com.misoo.pk01.myService.MY_S_EVENT");
protectedfinal IntentFilter filter=newIntentFilter(MY_S_EVENT);
private BroadcastReceiver receiver=new myIntentReceiver();
publicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = newLinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = newButton(this);
btn.setId(101);
btn.setText("play");
btn.setBackgroundResource(R.drawable.heart);
btn.setOnClickListener(this);
LinearLayout.LayoutParams param =
newLinearLayout.LayoutParams(80, 50);
param.topMargin = 10;
layout.addView(btn,param);
btn2 = newButton(this);
btn2.setId(102);
btn2.setText("stop");
btn2.setBackgroundResource(R.drawable.heart);
btn2.setOnClickListener(this);
layout.addView(btn2,param);
btn3 = newButton(this);
btn3.setId(103);
btn3.setText("exit");
btn3.setBackgroundResource(R.drawable.cloud);
btn3.setOnClickListener(this);
layout.addView(btn3,param);
tv = newTextView(this);
tv.setText("Ready");
LinearLayout.LayoutParams param2 = new
LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addView(tv,param2);
setContentView(layout);
//---------------------------------
registerReceiver(receiver, filter);
//------------------------------------------------------
bindService(newIntent("com.misoo.pk01.REMOTE_SERVICE"),
mConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
publicvoidonServiceConnected(ComponentName className, IBinder
ibinder) {
ib = ibinder;
}
@Override
publicvoidonServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
publicvoid onClick(View v) {
switch(v.getId()) {
case101:
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try{
ib.transact(1,data, reply, 0);
} catch (Exception e) {
e.printStackTrace();
}
break;
case102:
data = Parcel.obtain();
reply = Parcel.obtain();
try{
ib.transact(2,data, reply, 0);
} catch (Exception e) {
e.printStackTrace();
}
break;
case103:
finish();
break;
}
}
//----------------------------------------------------
class myIntentReceiver extends BroadcastReceiver {
@Override
publicvoidonReceive(Context context, Intent intent) {
intbn = intent.getIntExtra("key",-1);
if(bn== 0)
tv.setText("Playing");
else
tv.setText("Stop.");
}
}
}
// myService.java
package com.misoo.pk01;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.*;
import android.util.Log;
publicclass myService extends Service implements Runnable {
private IBinder mBinder = null;
private Thread th1;
publicstaticHandler h;
private MediaPlayer mPlayer = null;
publicstaticContext ctx;
privatefinal String MY_S_EVENT =
newString("com.misoo.pk01.myService.MY_S_EVENT");
@Override
publicvoidonCreate() {
super.onCreate();
ctx= this;
mBinder= new myBinder();
//---------------------------------
// 诞生一个子线程及其MQ
// 等待Message
//---------------------------------
th1 = newThread(this);
th1.start();
}
@Override
publicIBinder onBind(Intent intent) {
returnmBinder;
}
publicvoidrun() {
Looper.prepare();
h = newEventHandler(Looper.myLooper());
Looper.loop();
}
//---------------------------------------
classEventHandler extends Handler {
public EventHandler(Looper looper) {
super(looper);
}
publicvoid handleMessage(Message msg) {
String obj = (String)msg.obj;
if(obj.contains("play"))
{
if(mPlayer!= null) return;
//----------------------------------
Intent in = new Intent(MY_S_EVENT);
in.putExtra("key", 0);
ctx.sendBroadcast(in);
//----------------------------------
mPlayer= MediaPlayer.create(ctx, R.raw.dreamed);
try{
mPlayer.start();
} catch(Exception e) {
Log.e("Play", "error: " + e.getMessage(), e);
}
}
elseif(obj.contains("stop")) {
if(mPlayer != null) {
//----------------------------------
Intent in = newIntent(MY_S_EVENT);
in.putExtra("key", 1);
ctx.sendBroadcast(in);
//----------------------------------
mPlayer.stop();
mPlayer.release();
mPlayer= null;
}
}
}}}
// myBinder.java
package com.misoo.pk01;
import android.os.Binder;
import android.os.Message;
import android.os.Parcel;
publicclass myBinder extends Binder{
@Override
publicboolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws android.os.RemoteException {
switch(code){
case 1:
// 将Message丢到子线程的MQ to draw Graphic
String obj = "play";
Message msg = myService.h.obtainMessage(1,1,1,obj);
myService.h.sendMessage(msg);
break;
case 2:
// 将Message丢到子线程的MQ to stop drawing
obj = "stop";
msg = myService.h.obtainMessage(1,1,1,obj);
myService.h.sendMessage(msg);
break;
}
returntrue;
}}
// myActivity.java
package com.misoo.pk01;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
publicclass myActivity extends Activity implements OnClickListener {
privatefinalintWC = LinearLayout.LayoutParams.WRAP_CONTENT;
privatefinalintFP = LinearLayout.LayoutParams.FILL_PARENT;
private Button btn, btn2, btn3;
publicTextView tv;
private IBinder ib = null;
privatefinal String MY_S_EVENT =
newString("com.misoo.pk01.myService.MY_S_EVENT");
protectedfinal IntentFilter filter=newIntentFilter(MY_S_EVENT);
private BroadcastReceiver receiver=new myIntentReceiver();
publicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = newLinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = newButton(this);
btn.setId(101);
btn.setText("play");
btn.setBackgroundResource(R.drawable.heart);
btn.setOnClickListener(this);
LinearLayout.LayoutParams param =
newLinearLayout.LayoutParams(80, 50);
param.topMargin = 10;
layout.addView(btn,param);
btn2 = newButton(this);
btn2.setId(102);
btn2.setText("stop");
btn2.setBackgroundResource(R.drawable.heart);
btn2.setOnClickListener(this);
layout.addView(btn2,param);
btn3 = newButton(this);
btn3.setId(103);
btn3.setText("exit");
btn3.setBackgroundResource(R.drawable.cloud);
btn3.setOnClickListener(this);
layout.addView(btn3,param);
tv = newTextView(this);
tv.setText("Ready");
LinearLayout.LayoutParams param2 = new
LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addView(tv,param2);
setContentView(layout);
//---------------------------------
registerReceiver(receiver,filter);
//------------------------------------------------------
bindService(newIntent("com.misoo.pk01.REMOTE_SERVICE"),
mConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
publicvoidonServiceConnected(ComponentName className, IBinder
ibinder) {
ib = ibinder;
}
@Override
publicvoidonServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};
publicvoid onClick(View v) {
switch(v.getId()) {
case101:
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try{
ib.transact(1,data, reply, 0);
} catch (Exception e) {
e.printStackTrace();
}
break;
case102:
data = Parcel.obtain();
reply = Parcel.obtain();
try{
ib.transact(2,data, reply, 0);
} catch (Exception e) {
e.printStackTrace();
}
break;
case103:
finish();
break;
}
}
//----------------------------------------------------
class myIntentReceiver extends BroadcastReceiver {
@Override
publicvoidonReceive(Context context, Intent intent) {
intbn = intent.getIntExtra("key",-1);
if(bn== 0)
tv.setText("Playing");
else
tv.setText("Stop.");
}
}
}
// myService.java
package com.misoo.pk01;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.*;
import android.util.Log;
publicclass myService extends Service implements Runnable {
private IBinder mBinder = null;
private Thread th1;
publicstaticHandler h;
private MediaPlayer mPlayer = null;
publicstaticContext ctx;
privatefinal String MY_S_EVENT =
newString("com.misoo.pk01.myService.MY_S_EVENT");
@Override
publicvoidonCreate() {
super.onCreate();
ctx= this;
mBinder= new myBinder();
//---------------------------------
// 诞生一个子线程及其MQ
// 等待Message
//---------------------------------
th1 = newThread(this);
th1.start();
}
@Override
publicIBinder onBind(Intent intent) {
returnmBinder;
}
publicvoidrun() {
Looper.prepare();
h = newEventHandler(Looper.myLooper());
Looper.loop();
}
//---------------------------------------
classEventHandler extends Handler {
public EventHandler(Looper looper) {
super(looper);
}
publicvoid handleMessage(Message msg) {
String obj = (String)msg.obj;
if(obj.contains("play"))
{
if(mPlayer!= null) return;
//----------------------------------
Intent in = newIntent(MY_S_EVENT);
in.putExtra("key", 0);
ctx.sendBroadcast(in);
//----------------------------------
mPlayer= MediaPlayer.create(ctx, R.raw.dreamed);
try{
mPlayer.start();
} catch(Exception e) {
Log.e("Play", "error: " + e.getMessage(), e);
}
}
elseif(obj.contains("stop")) {
if(mPlayer != null) {
//----------------------------------
Intent in = newIntent(MY_S_EVENT);
in.putExtra("key", 1);
ctx.sendBroadcast(in);
//----------------------------------
mPlayer.stop();
mPlayer.release();
mPlayer= null;
}
}
}}}
// myBinder.java
package com.misoo.pk01;
import android.os.Binder;
import android.os.Message;
import android.os.Parcel;
publicclass myBinder extends Binder{
@Override
publicboolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws android.os.RemoteException {
switch(code){
case 1:
// 将Message丢到子线程的MQ to draw Graphic
String obj = "play";
Message msg = myService.h.obtainMessage(1,1,1,obj);
myService.h.sendMessage(msg);
break;
case 2:
// 将Message丢到子线程的MQ to stop drawing
obj = "stop";
msg = myService.h.obtainMessage(1,1,1,obj);
myService.h.sendMessage(msg);
break;
}
returntrue;
}}
~ End ~