java – Android set application launches display failed-ThrowExceptions

Exception or error:

My goal is to launch the application in Virtual Display, I am trying to build the virtual display in my first application.

mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        mDisplayRealMetrics = new DisplayMetrics();

        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getRealMetrics(mDisplayRealMetrics);

        mVirtualDisplay = mDisplayManager.createVirtualDisplay("Test Virtual Display",
                mVirtualDispalyWidth, mVirtualDisplayHeight, mDisplayRealMetrics.densityDpi,
                surface, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION,
                null, null
        );

Then I build anthor application and set android:exported="true" in this application’s manifest.

In my first application, I create a button and click button event.

setupVirtualDisplay();

Intent intent = new Intent( );
intent.setClassName(  "com.example.test","com.example.test.MainActivity");
Bundle bundle = new Bundle(  );
bundle = ActivityOptions.makeBasic().setLaunchDisplayId( mVirtualDisplay.getDisplay().getDisplayId() ).toBundle();
intent.setFlags( Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent, bundle );

Error:

2020-02-23 16:47:26.276 7227-7227/com.example.testvirtualdisplay E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testvirtualdisplay, PID: 7227
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.view.View$DeclaredOnClickListener.onClick(View.java:6430)
        at android.view.View.performClick(View.java:7870)
        at android.widget.TextView.performClick(TextView.java:14956)
        at android.view.View.performClickInternal(View.java:7839)
        at android.view.View.access$3600(View.java:886)
        at android.view.View$PerformClick.run(View.java:29315)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7770)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.view.View$DeclaredOnClickListener.onClick(View.java:6425)
        at android.view.View.performClick(View.java:7870) 
        at android.widget.TextView.performClick(TextView.java:14956) 
        at android.view.View.performClickInternal(View.java:7839) 
        at android.view.View.access$3600(View.java:886) 
        at android.view.View$PerformClick.run(View.java:29315) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7770) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047) 
     Caused by: java.lang.SecurityException: Permission Denial: starting Intent { flg=0x18000000 cmp=com.example.test/.MainActivity } from ProcessRecord{cd3c4d4 7227:com.example.testvirtualdisplay/u0a319} (pid=7227, uid=10319) with launchDisplayId=11
        at android.os.Parcel.createException(Parcel.java:2088)
        at android.os.Parcel.readException(Parcel.java:2056)
        at android.os.Parcel.readException(Parcel.java:2004)
        at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:4260)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1713)
        at android.app.Activity.startActivityForResult(Activity.java:5276)
        at android.app.Activity.startActivity(Activity.java:5601)
        at com.example.testvirtualdisplay.MainActivity.onClickButton(MainActivity.java:133)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.view.View$DeclaredOnClickListener.onClick(View.java:6425) 
        at android.view.View.performClick(View.java:7870) 
        at android.widget.TextView.performClick(TextView.java:14956) 
        at android.view.View.performClickInternal(View.java:7839) 
        at android.view.View.access$3600(View.java:886) 
        at android.view.View$PerformClick.run(View.java:29315) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7770) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047) 
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.wm.SafeActivityOptions.checkPermissions(SafeActivityOptions.java:217)
        at com.android.server.wm.SafeActivityOptions.getOptions(SafeActivityOptions.java:123)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:909)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:666)
        at com.android.server.wm.ActivityStarter.startActivityMayWait(ActivityStarter.java:1749)

Please make me know what’s happen???

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *