1

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

我试图将应用程序作为一项服务来保持应用程序在后台运行,但 mediarouter 回调似乎只在主线程上运行(我不确定我只是 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));
          }
        }                                                           
    }

该应用程序很好,它在主屏幕中显示一个视图,在第二个屏幕中显示第二个视图,但是当我将应用程序恢复为背景时,第二个屏幕会采用与第一个屏幕相同的视图。即使我恢复应用程序以使用另一个应用程序,我也想保持第二个视图显示在第二个屏幕中

4

0 回答 0