【问题标题】:Why Service Connection is not called in onResume?为什么 onResume 中不调用服务连接?
【发布时间】:2016-03-30 13:45:17
【问题描述】:

我正在开发后台服务,我正在更新服务中的一个 Arraylist,我从 Arraylist 获取索引并播放歌曲。

所以现在的问题是,如果我打算进行另一个活动,并且在该活动中,我正在将 Arraylist 更新为服务,但是当我完成活动并返回 MainActivity 时,它不会再次更新 Arraylist。

我正在使用领域数组列表来存储数据。

@Override
protected void onStart() {
    super.onStart();
    songConnection();

    if (playIntent == null) {
        playIntent = new Intent(this, MusicService.class);
        bindService(playIntent, musicServiceConn, Context.BIND_AUTO_CREATE);
        startService(playIntent);
    }
}

@Override
protected void onResume() {
    super.onResume();
    songConnection();
}

public void songConnection() {
    musicServiceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
            musicSrv = binder.getService();
            musicSrv.setList(musicRealmResults);
            musicBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicBound = false;
        }
    };
}

这里的 musicRealmResults 是 Realm Arraylist。

在 Albums.java 中

musicRealmResults = realm.where(Music.class).equalTo("albums", albums).findAllSorted("albums", Sort.ASCENDING);

在 MainActivity.java 中

musicRealmResults = realm.where(Music.class).findAll();

因此,在这两个活动中,Arraylist 的大小会有所不同,并且根据它应该更新到服务的大小。

所以请告诉我为什么活动结束时它没有在 onResume 中更新。

请仔细阅读我的帖子并建议我一些解决方案。

【问题讨论】:

  • 你根本没有在 SongConnection() 中启动你的服务
  • 提供更多代码。您在哪里开始/初始化您的服务?
  • musicSrv.setList(musicRealmResults); musicRealmResults 来自哪里?当您回到之前的活动时,它会更新吗?
  • @Jois 感谢您的回复,我已经更新了我的帖子,请仔细阅读,我正在 onStart 中启动服务
  • 一开始它会从 Realm DB 中的 SD 卡存储中获取所有音乐,我将数组列表的名称赋予 musicRealmResults,在这个数组列表中有四种类型的专辑、艺术家、歌曲和流派,如果您在专辑活动中,那么它将获取专辑列表,但是当我完成活动时,它不会更新数组列表。

标签: android service onresume realm


【解决方案1】:

像这样更改您的代码:

@Override
protected void onStart() {

    songConnection();// call this method before calling super

super.onResume();
}

public void songConnection() {
    musicServiceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
            musicSrv = binder.getService();
            musicSrv.setList(musicRealmResults);
            musicBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicBound = false;
        }
    };
 if (playIntent == null) {
        playIntent = new Intent(this, MusicService.class);
        bindService(playIntent, musicServiceConn, Context.BIND_AUTO_CREATE);
        startService(playIntent);
    }

}

现在您的服务何时会正确调用但请确保您在服务之前的某个时间点停止服务' s 重新开始。 谢谢,希望对您有所帮助。

【讨论】:

  • 嘿,谢谢,但还是一样,它没有更新 musicRealmResults 仍然是旧的大小,它应该更新服务的大小。
  • 你是否也改变了你的 onResume 方法??我已经改了看看
  • 是的,我在 super.onResume 之前更新了 onResume,只是我在调用该方法
  • 我现在确实阅读了一些博客。很抱歉,在 onRESUME() 中启动服务是不合适的,所以我认为最好将整个代码放在 onStart() 中
  • 但是试试我现在改变的! stackoverflow.com/questions/2304086/…
猜你喜欢
  • 2017-03-22
  • 1970-01-01
  • 2016-05-14
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
相关资源
最近更新 更多