Android App的源代码

 ee                                                    ee

// 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;

  ee                                                    ee

public class myActivity extends Activity implements OnClickListener {

        private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;

        private final int FP = LinearLayout.LayoutParams.FILL_PARENT;

        private Button btn, btn2, btn3;

        public TextView tv;

        private IBinder ib = null;

        private final String MY_S_EVENT =

         new String("com.misoo.pk01.myService.MY_S_EVENT");

        protected final IntentFilter filter=new IntentFilter(MY_S_EVENT);

        private BroadcastReceiver receiver=new myIntentReceiver();

 

        public void onCreate(Bundle icicle) {

                super.onCreate(icicle);

       

                LinearLayout layout = new LinearLayout(this);

                layout.setOrientation(LinearLayout.VERTICAL);

 

                btn = new Button(this);

                btn.setId(101);

                btn.setText("play");

                btn.setBackgroundResource(R.drawable.heart);

                btn.setOnClickListener(this);

                LinearLayout.LayoutParams param =

new LinearLayout.LayoutParams(80, 50);

                param.topMargin = 10;

                layout.addView(btn, param);

 

                btn2 = new Button(this);

                btn2.setId(102);

                btn2.setText("stop");

                btn2.setBackgroundResource(R.drawable.heart);

                btn2.setOnClickListener(this);

                layout.addView(btn2, param);

 

                btn3 = new Button(this);

                btn3.setId(103);

                btn3.setText("exit");

                btn3.setBackgroundResource(R.drawable.cloud);

                btn3.setOnClickListener(this);

                layout.addView(btn3, param);

 

                tv = new TextView(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(new Intent("com.misoo.pk01.REMOTE_SERVICE"),

                                mConnection, Context.BIND_AUTO_CREATE);

        }

        private ServiceConnection mConnection = new ServiceConnection() {

                @Override

                public void onServiceConnected(ComponentName className, IBinder

ibinder) {

                    ib = ibinder;

                }

                @Override

                public void onServiceDisconnected(ComponentName name) {

                        // TODO Auto-generated method stub

                       

                }

    };

    public void onClick(View v) {

                switch (v.getId()) {

                case 101:

                        Parcel data = Parcel.obtain();

                        Parcel reply = Parcel.obtain();

                        try {

                                ib.transact(1, data, reply, 0);

                        } catch (Exception e) {

                                e.printStackTrace();

                        }

                        break;

                case 102:

                        data = Parcel.obtain();

                        reply = Parcel.obtain();

                        try {

                                        ib.transact(2, data, reply, 0);

                                } catch (Exception e) {

                                        e.printStackTrace();

                                }

                        break;

                case 103:

                        finish();

                        break;

                }

        }

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

    class myIntentReceiver extends BroadcastReceiver {

                @Override

                public void onReceive(Context context, Intent intent) {

                        int bn = intent.getIntExtra("key",-1);

                        if(bn == 0)

                            tv.setText("Playing");

                        else

                                tv.setText("Stop.");

                }

        }

}

  ee                                                    ee

// 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;

  ee                                                           ee

public class myService extends Service implements Runnable {

        private IBinder mBinder = null;

        private Thread th1;

        public static Handler h;

        private MediaPlayer mPlayer = null;

        public static Context ctx;

        private final String MY_S_EVENT =

             new String("com.misoo.pk01.myService.MY_S_EVENT");

 

        @Override

        public void onCreate() {

                super.onCreate();

                 ctx = this;

                 mBinder = new myBinder();

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

                 // 诞生一个子线程及其MQ

                 // 等待Message

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

                 th1 = new Thread(this);

                th1.start();

    }

        @Override

        public IBinder onBind(Intent intent) {

                return mBinder;

        }

       

        public void run() {

             Looper.prepare();

                h = new EventHandler(Looper.myLooper());

           Looper.loop();

    }

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

        class EventHandler extends Handler {

        public EventHandler(Looper looper) {

                         super(looper);

        }

        public void 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);

                                }

                       

                 }

                 else if(obj.contains("stop")) {

                         if (mPlayer != null) {

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

                                  Intent in = new Intent(MY_S_EVENT);

                                  in.putExtra("key", 1);

                                  ctx.sendBroadcast(in);      

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

                            mPlayer.stop();

                                 mPlayer.release();

                                 mPlayer = null;

                         }

               }

    }}}

 ee                                                    ee

// myBinder.java

package com.misoo.pk01;

import android.os.Binder;

import android.os.Message;

import android.os.Parcel;

 

public class myBinder extends Binder{

        @Override

        public boolean 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;

                       }

                       return true;

        }}

// ----------- END --------------------------------------------------------------