服务com.google.android.youtube.api.service.YouTubeService泄漏了IntentReceiver-集成YouTube API时出错

时间:2018-12-28 10:57:59

标签: java android memory-leaks youtube-api

我尝试在具有多个视频的ViewPager中的Android应用程序中实现YouTube API。问题是,一旦我滑动到ViewPager中的第二个视频,就会收到此内存泄漏错误:

Service com.google.android.youtube.api.service.YouTubeService has leaked IntentReceiver aeny@5c159ff that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Service com.google.android.youtube.api.service.YouTubeService has leaked IntentReceiver aeny@5c159ff that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:928)
        at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:725)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1182)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1162)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1156)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:564)

我在网上到处搜索了每个堆栈溢出问题,但无法纠正该问题。我参考的一些地方:

片段类

public class YouTubePlayerFragment extends Fragment implements YouTubePlayer.OnInitializedListener {
    YouTubePlayerSupportFragment youtubeFragment;
    YouTubePlayer player;

    String video;

    boolean isFullScreen = false;

    private static final int YOUTUBE_RECOVERY_REQUEST = 1;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            video = bundle.getString("video_key");
        }

        return inflater.inflate(R.layout.fragment_youtube_video, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        youtubeFragment = YouTubePlayerSupportFragment.newInstance();

        youtubeFragment.initialize(Config.YOUTUBE_API_KEY, this);
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.youtube_fragment, youtubeFragment).commit();
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
        if (!wasRestored) {
            player = youTubePlayer;
            HomeActivity.youTubePlayerMain = youTubePlayer;
            player.setOnFullscreenListener(new YouTubePlayer.OnFullscreenListener() {
                @Override
                public void onFullscreen(boolean fullscreen) {
                    isFullScreen = fullscreen;
                    HomeActivity.isYouTubePlayerFullScreen = fullscreen;
                }
            });
            player.cueVideo(video);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError())
            errorReason.getErrorDialog(requireActivity(), YOUTUBE_RECOVERY_REQUEST).show();
        else {
            String errorMessage = String.format("There was an error initializing the YouTubePlayer (%1$s)", errorReason.toString());
            Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == YOUTUBE_RECOVERY_REQUEST)
            getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);
    }

    private YouTubePlayer.Provider getYouTubePlayerProvider() {
        return youtubeFragment;
    }
}

适配器类

public class PageViewerAdapter extends FragmentStatePagerAdapter {
    private List<String> videoList;

    PageViewerAdapter(FragmentManager fm, List<String> videoList) {
        super(fm);
        this.videoList = videoList;
    }

    @Override
    public Fragment getItem(int position) {
        Bundle bundle = new Bundle();
        bundle.putString("video_key", videoList.get(position));
        YouTubePlayerFragment fragment = new YouTubePlayerFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getCount() {
        return videoList.size();
    }
}

现在的问题是,如果根据错误,如果我使用unregisterReceiver(),则应在此方法的参数中添加什么。我还没有创建任何广播接收器。

0 个答案:

没有答案