解决方案 »

  1.   

    你弄个map映射一下就好了吧
      

  2.   

    把音乐列表传到Service里面
      

  3.   


    感谢回复!
    不好意思我实在十分小白,是这样子吗?我运行了一下,APP会闪退,不知道是哪里出了错?
      

  4.   

    当然闪退,你把map弄成静态变量,在onStartCommand里收到参数后再去取,你现在没取到参数直接用就报错,不能在onCreate里用
      

  5.   

    感谢大哥!我又改了改,但不知是不是我对这个“参数”是什么“参数”不太理解的原因(是指传过来的map的key吗?),结果还是闪退了,挠头.jpg以下是代码,可以再帮我看看吗?package com.example.myalarm;import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.IBinder;import java.util.HashMap;
    import java.util.Map;public class MusicService extends Service {
        private String Sring;
        public MusicService() {
        }
        static boolean isplay;
        MediaPlayer player;
        static Map<String,Integer> date = new HashMap<String,Integer>();
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (!player.isPlaying()) {
                player.start();
                isplay = player.isPlaying();
            }
            date.put("bird",R.raw.bird);
            date.put("chimes",R.raw.chimes);
            date.put("cock",R.raw.cock);
            date.put("cuckoo",R.raw.cuckoo);
            date.put("notify",R.raw.notify);
            date.put("ringout",R.raw.ringout);
            date.put("water",R.raw.water);
            Sring=intent.getStringExtra("Sring");//这算是收到了参数吗?
            return super.onStartCommand(intent, flags, startId);
        }    @Override
        public void onCreate() {
            player = MediaPlayer.create(this,date.get(Sring));
        }
        @Override
        public void onDestroy() { 
            player.stop(); 
            isplay = player.isPlaying(); 
            player.release(); 
            super.onDestroy();
        }
    }
      

  6.   

    生命周期不对,onCreate - > onStartCommand
      

  7.   

    感谢大哥!我又改了改,但不知是不是我对这个“参数”是什么“参数”不太理解的原因(是指传过来的map的key吗?),结果还是闪退了,挠头.jpg以下是代码,可以再帮我看看吗?package com.example.myalarm;import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.IBinder;import java.util.HashMap;
    import java.util.Map;public class MusicService extends Service {
        private String Sring;
        public MusicService() {
        }
        static boolean isplay;
        MediaPlayer player;
        static Map<String,Integer> date = new HashMap<String,Integer>();
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (!player.isPlaying()) {
                player.start();
                isplay = player.isPlaying();
            }
            date.put("bird",R.raw.bird);
            date.put("chimes",R.raw.chimes);
            date.put("cock",R.raw.cock);
            date.put("cuckoo",R.raw.cuckoo);
            date.put("notify",R.raw.notify);
            date.put("ringout",R.raw.ringout);
            date.put("water",R.raw.water);
            Sring=intent.getStringExtra("Sring");//这算是收到了参数吗?
            return super.onStartCommand(intent, flags, startId);
        }    @Override
        public void onCreate() {
            player = MediaPlayer.create(this,date.get(Sring));
        }
        @Override
        public void onDestroy() { 
            player.stop(); 
            isplay = player.isPlaying(); 
            player.release(); 
            super.onDestroy();
        }
    }你在onCreate里生成player可以,但不要在这里获取date.get(String),你可以先传一个Uri.EMPTY,然后在onStartCommand接收到参数再调用player.setDataSource(this, date.get(Sring))更新数据,然后播放
      

  8.   


    你在onCreate里生成player可以,但不要在这里获取date.get(String),你可以先传一个Uri.EMPTY,然后在onStartCommand接收到参数再调用player.setDataSource(this, date.get(Sring))更新数据,然后播放Uri这个我完全没学过相关内容,所以这个什么意思、是什么步骤……我,看不懂
      

  9.   

    感谢大哥!我又改了改,但不知是不是我对这个“参数”是什么“参数”不太理解的原因(是指传过来的map的key吗?),结果还是闪退了,挠头.jpg以下是代码,可以再帮我看看吗?package com.example.myalarm;import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.IBinder;import java.util.HashMap;
    import java.util.Map;public class MusicService extends Service {
        private String Sring;
        public MusicService() {
        }
        static boolean isplay;
        MediaPlayer player;
        static Map<String,Integer> date = new HashMap<String,Integer>();
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (!player.isPlaying()) {
                player.start();
                isplay = player.isPlaying();
            }
            date.put("bird",R.raw.bird);
            date.put("chimes",R.raw.chimes);
            date.put("cock",R.raw.cock);
            date.put("cuckoo",R.raw.cuckoo);
            date.put("notify",R.raw.notify);
            date.put("ringout",R.raw.ringout);
            date.put("water",R.raw.water);
            Sring=intent.getStringExtra("Sring");//这算是收到了参数吗?
            return super.onStartCommand(intent, flags, startId);
        }    @Override
        public void onCreate() {
            player = MediaPlayer.create(this,date.get(Sring));
        }
        @Override
        public void onDestroy() { 
            player.stop(); 
            isplay = player.isPlaying(); 
            player.release(); 
            super.onDestroy();
        }
    }你在onCreate里生成player可以,但不要在这里获取date.get(String),你可以先传一个Uri.EMPTY,然后在onStartCommand接收到参数再调用player.setDataSource(this, date.get(Sring))更新数据,然后播放Uri这个我完全没学过相关内容,所以这个什么意思、是什么步骤……我,看不懂
    引用错了,再发一次
      

  10.   

    感谢各位的回复,今天终于用十分傻瓜的方法弄出来了!
    惭愧,我基础确实十分薄弱,我校大概课程安排不太好,我们java几乎没怎么学,Android studio刚学一点,就要开始做APP了,时间紧迫没来得及好好从头学起,之后会再好好搞清楚的!