MainActivity

package com.kevin.test;

import com.kevin.test.R;
import com.kevin.test.ControlMusicService;
import com.kevin.test.IMusicControlService;

import android.app.Activity;
import android.app.AlertDialog;
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.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 //这个是一个普通的接口,对于程序员来说
 private ImageButton play,pause,stop;
 private MediaPlayer mp;
 ActivityReceiver activityreceiver;
 //private int  et=0;
 public IMusicControlService iMusicControlService;
 //BroadcastReceiver recevier;
 //ActivityReceiver activityreceiver;
 //public static final String UPDATE_ACTION="com.kevin.test.update_action";
 

 //private static final String BC_ACTION = "com.kevin.test.action.BC_ACTION1";
 //通过执行这个方法给接口赋实现类   serviceConnection是专门用来连接服务的
 //调用后台的服务,貌似如果不存在就启动,存在就调用
    private final ServiceConnection serviceConnection =new ServiceConnection(){
         //第一次链接的时候会调用这个方法
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   Log.i("kevin", "frist conn 1Myfi");
   iMusicControlService =IMusicControlService.Stub.asInterface(service);
  }
       //service断开的时候会调用这个方法
  @Override
  public void onServiceDisconnected(ComponentName name) {
   Log.i("kevin", "disconn");
    iMusicControlService = null;
  }
     
    };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
      
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       // mp = MediaPlayer.create(this, R.raw.big);
      
        Intent intent = new Intent();
        intent.setClass(this, ControlMusicService.class);
        boolean value = true;
//        intent.putExtra("tag", et);
        //把serviceConnection绑定到 ControlMusicService上
        //估计在绑定的时候就会创建后台service和获取service返回的对象
       
        bindService(intent,serviceConnection,Context.BIND_AUTO_CREATE);
        IntentFilter filter  =  new IntentFilter();
     filter.addAction(ControlMusicService.UPDATE_BUTTON);
     activityreceiver = new ActivityReceiver();
     registerReceiver(activityreceiver, filter);
        Log.i("kevin", "Bind service...");
        play = (ImageButton)findViewById(R.id.play);
        pause = (ImageButton)findViewById(R.id.pause);
        pause.setEnabled(false);
       // stop = (ImageButton)findViewById(R.id.stop);
       // setup();
//        recevier = new BroadcastReceiver() {
//   
//   @Override
//   public void onReceive(Context context, Intent intent) {
//    // TODO Auto-generated method stub
//    Log.i("kevin","Receive Broadcast");
//    play.setEnabled(true);
//   }
//  };
        play.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
    
    try {
     Log.i("kevin", "playmusic");
     //调用接口的方法
    
     play.setEnabled(false);
     pause.setEnabled(true);
    //stop.setEnabled(false);
     iMusicControlService.playMusic();
//     new Thread()
//     {
//      @Override
//      public void run()
//      {
//       while(true){
//       try {
//        Thread.sleep(5000);
//       }catch(InterruptedException e)
//       {
//       }
//       try {
//        if(iMusicControlService.Musicdone())
//         {Log.i("kevin","music done");
//       // play.setEnabled(true);
//         }
//        else Log.i("kevin","music undone");
//       } catch (RemoteException e) {
//        // TODO Auto-generated catch block
//        e.printStackTrace();
//       }
//       }
//       }
//      }.start();
     
     
     
     
     
    
    
   
  }catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
   }
        });
  
  pause.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
    try {
     Log.i("kevin", "pausemusic");
     //调用接口的方法
     play.setEnabled(true);
     pause.setEnabled(false);
     //stop.setEnabled(true);
     iMusicControlService.pauseMusic();
     
     
     
    } catch (RemoteException e) {
     e.printStackTrace();
    }
   }
  });
  
//  stop.setOnClickListener(new View.OnClickListener() {
//   public void onClick(View view) {
//    try {
//     Log.d("kevin", "stopmusic");
//     //调用接口的方法
//     play.setEnabled(true);
//     pause.setEnabled(false);
//     stop.setEnabled(false);
//     iMusicControlService.stopMusic();
//     
//     
//    } catch (RemoteException e) {
//     e.printStackTrace();
//    }
//   }
//  });
//       
  
    }
   
    public void onStop(){
     
    super.onStop();
    Log.i("kevin","stopmusic");
    //this.finish();
   
    }
   
   
    public class ActivityReceiver extends BroadcastReceiver{

       @Override 
            public void onReceive(Context context, Intent intent) { 
         Log.i("kevin", "msg:getaction");     
        String action = intent.getAction(); 
             if (action.equals(ControlMusicService.UPDATE_BUTTON)) { 
                 //从MusicService接收广播消息,弹出Toast 
                  String msg = intent.getStringExtra("msg"); 
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT) 
                            .show(); 
                    play.setEnabled(true);
                    pause.setEnabled(false);
               } 
           } 

     
    }

// @Override
// public void onCompletion(MediaPlayer mplayer) {
//  // TODO Auto-generated method stub
//  et=1;
// }

   // @Override
// public void onDestroy() {
//  super.onDestroy();
//  if (stop.isEnabled()) {
//   Log.i("kevin", "msg:stop onDestroy");
//   stop();
//  }
// }
// 
// public void onCompletion(MediaPlayer mp) {
//  stop();
// }
// 
 private void play() {
  mp.start();
  
  play.setEnabled(false);
  pause.setEnabled(true);
  stop.setEnabled(true);
 }
 
// private void stop() {
//  mp.stop();
//  pause.setEnabled(false);
//  stop.setEnabled(false);
//  
//  try {
//   mp.prepare();
//   mp.seekTo(0);
//   play.setEnabled(true);
//  }
//  catch (Throwable t) {
//   error(t);
//  }
// }
// 
 private void pause() {
  mp.pause();
  play.setEnabled(true);
  pause.setEnabled(false);
  stop.setEnabled(true);
 }
 
// private void loadClip() {
//  try {
//   mp=MediaPlayer.create(this, R.raw.big);
//   //mp.setOnCompletionListener(this);
//  }
//  catch (Throwable t) {
//   error(t);
//  }
// }
// 
// private void setup() {
//  loadClip();
//  play.setEnabled(true);
//  pause.setEnabled(false);
//  stop.setEnabled(false);
// }
// 
// private void error(Throwable t) {
//  AlertDialog.Builder builder=new AlertDialog.Builder(this);
//  builder
//   .setTitle("报错啦!")
//   .setMessage(t.toString())
//   .setPositiveButton("确定", null)
//   .show();
// }
}

你可能感兴趣的:(MainActivity)