首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ServiceConnection onServiceConnected没有调用

ServiceConnection onServiceConnected没有调用
EN

Stack Overflow用户
提问于 2016-10-02 12:32:49
回答 1查看 666关注 0票数 0

获取Service对象上的空对象错误。此方法未初始化。

private ServiceConnection musicConnection = new ServiceConnection()

它的containg选项卡主机然后是这个Fragment

代码语言:javascript
复制
public class PrimaryFragment extends Fragment {
    RecyclerView rv;
    LazyAdapter adapter;
    ListView listView;
    View rootView;
    private MusicService serviceMusic;
    ArrayList<AudioListModel> songsList;
    private Intent playIntent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.primary_layout, container, false);

        songsList = SongsManager.GetSongs();

        adapter = new LazyAdapter(getActivity(), songsList.toArray(new AudioListModel[songsList.size()]));
        //rv = (RecyclerView) rootView.findViewById(R.id.songs_recycleview);

        listView = (ListView) rootView.findViewById(R.id.SongList);
        LazyAdapter ad = new LazyAdapter(getActivity(), songsList.toArray(new AudioListModel[songsList.size()]));

        listView.setItemsCanFocus(false);
        listView.setAdapter(ad);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Toast.makeText(getActivity(), position + "this is on click event", Toast.LENGTH_SHORT).show();

                serviceMusic.setSelectedSong(position, MusicService.NOTIFICATION_ID); // getting error here......

                Intent i = new Intent(getActivity(), PlaySongActivity.class);
                startActivity(i);
            }
        });

        return rootView;
    }

    // This method is not initializing. 
    private ServiceConnection musicConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.PlayerBinder binder = (MusicService.PlayerBinder) service; 
            //get servic1
            serviceMusic = binder.getService();
            serviceMusic.setSongList(songsList);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    public void onStart() {
        super.onStart();
        //Start service
        //Toast.makeText(getActivity(), "before onStart", Toast.LENGTH_SHORT).show();
        if (playIntent == null) {
            //Toast.makeText(getActivity(), "after onStart", Toast.LENGTH_SHORT).show();
            playIntent = new Intent(getActivity(), MusicService.class);
            getActivity().bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
            getActivity().startService(playIntent);
        }
    }
}

这是音乐服务班

代码语言:javascript
复制
public class MusicService extends Service implements
    MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
    MediaPlayer.OnCompletionListener {

private MediaPlayer mPlayer;
private Uri mSongUri;

private ArrayList<ListModel> mListSongs;
private int SONG_POS = 0;

private final IBinder musicBind = new PlayerBinder();


private Notification.Builder notificationBuilder;
private Notification mNotification;

public class PlayerBinder extends Binder {//Service connection to play in background

    public MusicService getService() {
        Log.d("test", "getService()");
        return MusicService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    Log.d("test", "onBind Called ");
    return musicBind;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-02 14:56:46

我没有在“MusicService”中注册"AndroidManifest.xml“。所以我把这条线包括进去,它就开始起作用了。"< service android:name=".MusicService“/>”我们需要在清单中注册服务。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39817040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档