带有singleton类的Android Observable

时间:2017-03-16 07:05:44

标签: android singleton observable observer-pattern

我有以下

  1. 活动A作为观察员
  2. 作为观察员的活动B
  3. 单例类C用于媒体播放器作为可观察的。单曲类在歌曲完成时通知观察者(活动A和B)。 但是,一次只通知一个活动 例如
  4. 当应用程序启动时,我首先进入活动A并使用单例C类播放歌曲,当歌曲完成时会通知
  5. 当应用程序第一次启动时,我首先进入活动B并使用单件C类播放歌曲,当歌曲完成时会通知它。
  6. 我按下活动A中的后退按钮返回主菜单,然后转到活动B,只通知活动A.
  7. 我按下活动B中的后退按钮返回主菜单,然后转到活动A,只通知活动B.
  8. 对于方案1& 2,如果只通知一个Activity,因为其他Activity尚未打开,则可以,

    对于方案3& 4,两个活动都已打开,但只会通知首先打开的活动。

    我是否遗漏了代码中的任何内容以通知两位观察员?

    public class StartActivity extends ListActivity {
    .....
    
       @Override
        public void onListItemClick(ListView parent, View v, int position, long id) {
    
            if (position == 1){
            Intent myIntent = new Intent(StartActivity.this, download.class);
            StartActivity.this.startActivity(myIntent);
        }
    
            if (position + 1 == 2){
                Intent myIntent = new Intent(StartActivity.this, qirath.class);
                StartActivity.this.startActivity(myIntent);
            }
    
    
    }
    
    public class download extends ListActivity implements Observer,
            OnSeekBarChangeListener {
            ......
        private mediaManager player ;
        @Override
        public void update(Observable o, Object arg) {
            System.out.println("download called");
    
        }
           @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                    if (player == null) player = getInstance();
                    player.addObserver(this);
            }
        }
    
    
    public class qirath extends ListActivity implements Observer,
            OnSeekBarChangeListener {
            ......
        private mediaManager player ;
        @Override
        public void update(Observable o, Object arg) {
            System.out.println("qirath called");
    
        }
           @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                    if (player == null) player = getInstance();
                    player.addObserver(this);
            }
    }
    
    public class mediaManager  extends Observable implements  AudioManager.OnAudioFocusChangeListener, OnCompletionListener, MediaPlayer.OnPreparedListener,
            MediaPlayer.OnErrorListener {
        private static mediaManager ourInstance;
        MediaPlayer player =null;
         public static mediaManager getInstance()
        {
            if (ourInstance == null) {
                ourInstance = new mediaManager();
                return ourInstance;
            }
            else {
                return ourInstance;
            }
        }
          void createMediaPlayerIfNeeded() {
            if (player == null) {
                player = new MediaPlayer();
                player.setOnPreparedListener(this);
                player.setOnCompletionListener(this);
                player.setOnErrorListener(this);
    
            }
            else
                player.reset();
        }
    .......
        public void onCompletion(MediaPlayer player) {
            // media player finished current song
            setChanged();
            notifyObservers("c");
        }
        .....
    }
    

0 个答案:

没有答案