1

please I need your help after searching a lot without issues. I have an demostration app to use an second screen attached to my device. I have the source code of the app, they use the Mediarouter class and an class named LauncherSecondScreen extended from the Presentation class

I have tried to make the app as an service to keep runnig the app in background, but the mediarouter callback seems running only on the princpal thread ( I'm not sure I am just a beginner in android dev). I have the full code of the app : there is two layout activity one showed on the princpal screen and the other on the second screen:

    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));
          }
        }                                                           
    }

the app is well, it make one view in the princpale screen and a second view in the second screen , but when i resume the app to background the second screen take the same view of the first screen. I want to keep the second view showing in the second screen even i resume the app to use another app

Tenusha Guruge
  • 2,147
  • 3
  • 18
  • 38
SlimSof
  • 11
  • 1

0 Answers0