如何保持MediaRouter在后台活动中运行?

时间:2019-06-17 10:58:05

标签: java android presentation mediarouter

请在搜索很多没有问题后需要我的帮助。 我有一个演示应用程序,可以使用连接到设备的第二个屏幕。 我有应用程序的源代码,它们使用Mediarouter类和从Presentation类扩展的名为LauncherSecondScreen的类

我试图使该应用程序成为一种服务,以使该应用程序在后台运行,但是mediarouter回调似乎仅在princpal线程上运行(我不确定我只是android dev的初学者)。 我有该应用程序的完整代码:有两个布局活动,一个显示在主屏幕上,另一个显示在第二个屏幕上:

    public class MainActivity extends Activity {

        private final String TAG = "PresentationWithMediaRouterActivity";
        private MediaRouter mMediaRouter;
        private LauncherSecondScreen mPresentation;
        private boolean mPaused;

        /**
         * Initialization of the Activity after it is first created.  Must at least
         * call {@link android.app.Activity#setContentView setContentView()} to
         * describe what is to be displayed in the screen.
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // Be sure to call the super class.
            super.onCreate(savedInstanceState);

            // Get the media router service.
            mMediaRouter = (MediaRouter)getSystemService(Context.MEDIA_ROUTER_SERVICE);

            // See assets/res/any/layout/presentation_with_media_router_activity.xml for this
            // view layout definition, which is being set here as
            // the content of our screen.
            setContentView(R.layout.activity_main);

        }

        @Override
        protected void onResume() {
            // Be sure to call the super class.
            super.onResume();

            // Listen for changes to media routes.
            mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouterCallback);

            // Update the presentation based on the currently selected route.
            mPaused = false;
            updatePresentation();
        }

        private void updatePresentation() {
            // Get the current route and its presentation display.
            MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(
                    MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
            Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;

            // Dismiss the current presentation if the display has changed.
            if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) {
                Log.i(TAG, "Dismissing presentation because the current route no longer "
                        + "has a presentation display.");
                mPresentation.dismiss();
                mPresentation = null;
            }

            // Show a new presentation if needed.
            if (mPresentation == null && presentationDisplay != null) {
                Log.i(TAG, "Showing presentation on display: " + presentationDisplay);
                mPresentation = new LauncherSecondScreen(this, presentationDisplay);
                mPresentation.setOnDismissListener(mOnDismissListener);
                try {
                    mPresentation.show();
                } catch (WindowManager.InvalidDisplayException ex) {
                    Log.w(TAG, "Couldn't show presentation!  Display was removed in "
                            + "the meantime.", ex);
                    mPresentation = null;
                }
            }

            // Update the contents playing in this activity.
            updateContents();
        }

        private void updateContents() {
            // Show either the content in the main activity or the content in the presentation
            // along with some descriptive text about what is happening.
            if (mPresentation != null) {


                if (mPaused) {
                    mPresentation.dismiss();//getSurfaceView().onPause();
                } else {
                    mPresentation.show();//getSurfaceView().onResume();
                }
            } else {
               /* mInfoTextView.setText("presentation_with_media_router_now_playing_locally");
                mSurfaceView.setVisibility(View.VISIBLE);
                if (mPaused) {
                    mSurfaceView.onPause();
                } else {
                    mSurfaceView.onResume();
                }*/
            }
        }

        private final MediaRouter.SimpleCallback mMediaRouterCallback =
                new MediaRouter.SimpleCallback() {
                    @Override
                    public void onRouteSelected(MediaRouter router, int type, RouteInfo info) {
                        Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info);
                        updatePresentation();
                    }

                    @Override
                    public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) {
                        Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info);
                        updatePresentation();
                    }

                    @Override
                    public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) {
                        Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info);
                        updatePresentation();
                    }
                };

        /**
         * Listens for when presentations are dismissed.
         */
        private final DialogInterface.OnDismissListener mOnDismissListener =
                new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (dialog == mPresentation) {
                            Log.i(TAG, "Presentation was dismissed.");
                            mPresentation = null;
                            updateContents();
                        }
                    }
                };
      @SuppressLint({"NewApi"})
        public class LauncherSecondScreen extends Presentation
        {
          public LauncherSecondScreen(Context paramContext, Display paramDisplay)
          {
            super(paramContext, paramDisplay/*,android.R.style.Theme_Holo_Light_Dialog_NoActionBar*/);
          }

          protected void onCreate(Bundle paramBundle)
          {
            super.onCreate(paramBundle);
            setContentView(R.layout.dialog_second_screen_content);
         ////   this.iv_secondScreen_banner = ((ImageView)findViewById(R.id.titleImage));
          }
        }                                                           
    }

该应用程序很好,它在princpale屏幕中显示了一个视图,在第二个屏幕中显示了第二个视图,但是当我将应用程序恢复为背景时,第二个屏幕将显示第一个屏幕的相同视图。 我想让第二个视图仍显示在第二个屏幕中,即使我继续使用该应用程序以使用另一个应用程序

0 个答案:

没有答案