Android 沒法Bind Service

      今天幫同窗看一個bindService的樣例,說是沒法bind service(他接觸android時間不長),那代碼是從網上找的一個樣例,結果看了半天,才終於找到問題所在了),這個問題真是使人挺無語的!下面貼代碼:java

MusicService.javaandroid

 

package com.pan.music;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MusicService extends Service {

    /** 音樂存放路徑 */
    private static final String MUSIC_PATH = "/sdcard/music";

    private List<String> muiscList;
    private MediaPlayer mediaPlayer;

    /** 服務綁定 */
    public class MusicBinder extends Binder {
        public MusicService getMusicService() {
            return MusicService.this;
        }
    }

    private MusicBinder musicBinder; //錯誤代碼,都沒有new一個對象,致使那邊獲取到的對象爲null,正確代碼以下:private MusicBinder musicBinder = new MusicBinder();
    
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("INFO", "onBind");
        return null;  //此處直接返回了一個null,結果onServiceConnected方法中,獲取到的值始終爲null,應該return musicBinder;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Log.d("Info", "sfhahhhhhhhhhhhh");
        super.onCreate();
    }
    

    
    
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("Info", "sfhahhhhhhhhhhhh");
        super.onStartCommand(intent, flags, startId);
        mediaPlayer = new MediaPlayer();
        // musicListInit();
        // playing(0);

        return 1;
    }
    
//    @Override
//    public void onStart(Intent intent, int startId) {
//        // TODO Auto-generated method stub
//        
//        mediaPlayer = new MediaPlayer();
//    }

    public void execute() {
        Log.i("Info", "execute aaaaa");
    }
    
    private void musicListInit() {
        muiscList = new ArrayList<String>(10);
        File objFile = new File(MUSIC_PATH);
        String[] strMusicNames = objFile.list();
        for (String strName : strMusicNames) {
            muiscList.add(strName);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (null != mediaPlayer) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    private void playing(int curent) {
        // mediaPlayer = MediaPlayer.create(this, R.raw.test);
        // AssetFileDescriptor fileDescriptor = null;
        try {
            // fileDescriptor = getAssets().openFd("test.mp3");
            // mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor());
            mediaPlayer.reset();
            String strMusicName = muiscList.get(curent);
            mediaPlayer.setDataSource(MUSIC_PATH + "/" + strMusicName);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
        OnCompletionListener listener = new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                playing(2);
            }

        };
        mediaPlayer.setOnCompletionListener(listener);
    }
}

MainActivity.java 在這裏面綁定service,代碼以下:app

package com.pan.music;

import com.pan.music.MusicService.MusicBinder;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private MusicService musicService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button startButton = (Button) findViewById(R.id.start);
        startButton.setOnClickListener(startClick);
        Button stopButton = (Button) findViewById(R.id.stop);
        stopButton.setOnClickListener(stopClick);
//         Intent objIntent = new Intent(MainActivity.this, MusicService.class);
//         this.bindService(objIntent, mConnection,
//         Context.BIND_AUTO_CREATE);

    }

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("info", "zhixing");
            musicService = ((MusicBinder) service)
                    .getMusicService();
            //正由於service返回的爲空,因此這裏面始終都沒有進來。 if (musicService != null) {
                Log.i("info", "music service connect aaaa");
                musicService.execute();
            } else {
                Log.i("info", "music service connect aaa");
            }
            
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService = null;
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private OnClickListener startClick = new OnClickListener() {
        public void onClick(View view) {
            // Intent objIntent = new
            // Intent("com.pan.music.service.MusicService");
            Intent objIntent = new Intent(MainActivity.this, MusicService.class);
            // startService(objIntent);
            // musicConnection = new MusicConnection();
            bindService(objIntent, mConnection, Context.BIND_AUTO_CREATE);
            
        }
    };

    private OnClickListener stopClick = new OnClickListener() {
        public void onClick(View view) {
            // Intent objIntent = new
            // Intent("com.pan.music.service.MusicService");
//            Intent objIntent = new Intent(MainActivity.this, MusicService.class);
//            stopService(objIntent);
            unbindService(mConnection);
        }
    };

}

      這個問題其實很簡單,但對於初學者來講,這就很難找到問題的所在了,因此說貼出一個代碼出來,應該本身先調試下,看看有沒有問題,否則會給其餘人形成很大的困擾!ide

相關文章
相關標籤/搜索