• Android 无法Bind Service


          今天帮同学看一个bindService的样例,说是无法bind service(他接触android时间不长),那代码是从网上找的一个样例,结果看了半天,才终于找到问题所在了),这个问题真是令人挺无语的!下面贴代码:

    MusicService.java

    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,代码如下:

    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);
            }
        };
    
    }

          这个问题其实很简单,但对于初学者来说,这就很难找到问题的所在了,所以说贴出一个代码出来,应该自己先调试下,看看有没有问题,不然会给其他人造成很大的困扰!

  • 相关阅读:
    kubernetes----资源控制器5
    kubernetes----二进制安装3
    kubernetes----自动化安装2
    kubernetes----基础1
    PC微信低版本限制登录怎么办?
    mysql排名次
    mysql中GROUP BY中报错
    scrapy框架操作
    python QQ与微信自动发消息
    python中调用js的编码问题
  • 原文地址:https://www.cnblogs.com/tianyaxue/p/3183044.html
Copyright © 2020-2023  润新知