1

我有一个Activity,当第一次创建该 Activity 时,它从 转到 background( onPause) onResume,并且 Layout 不再可见。我尝试通过调用该活动startActivity,但没有用....任何帮助显示该活动布局????这是我的活动代码

public class 

    InComingCallActivity extends Activity { // do NOT extend from UC*Activity, we do not want to login on this screen!
        private static final String LOG_TAG = "InComingCallActivity";

        // only one instance of this class should be displayed
        // TODO find how to avoid multiple creations
        private static InComingCallActivity sharedInstance = null;



        private ImageView mCallingUserAvatarView;
        private TextView mRoomNameTextView;
        private Button mIgnoreCallButton;
        private Button mAcceptCallButton;
        private String mCallId;
        private String mMatrixId;
        private MXSession mSession;
        private IMXCall mMxCall;
        static Thread thread;

        private final IMXCall.MXCallListener mMxCallListener = new IMXCall.MXCallListener() {
            @Override
            public void onStateDidChange(String state) {
                Log.d(LOG_TAG, "## onStateDidChange(): state=" + state);
            }


            @Override
            public void onCallError(String aErrorMsg) {
                Log.d(LOG_TAG, "## dispatchOnCallError(): error=" + aErrorMsg);

                if (IMXCall.CALL_ERROR_USER_NOT_RESPONDING.equals(aErrorMsg)) {
                    CommonActivityUtils.displayToastOnUiThread(InComingCallActivity.this, InComingCallActivity.this.getString(R.string.call_error_user_not_responding));
                } else if (IMXCall.CALL_ERROR_ICE_FAILED.equals(aErrorMsg)) {
                    CommonActivityUtils.displayToastOnUiThread(InComingCallActivity.this, InComingCallActivity.this.getString(R.string.call_error_ice_failed));
                } else if (IMXCall.CALL_ERROR_CAMERA_INIT_FAILED.equals(aErrorMsg)) {
                    CommonActivityUtils.displayToastOnUiThread(InComingCallActivity.this, InComingCallActivity.this.getString(R.string.call_error_camera_init_failed));
                } else {
                    CommonActivityUtils.displayToastOnUiThread(InComingCallActivity.this, aErrorMsg);
                }
                //   thread.interrupt();
            }

            @Override
            public void onViewLoading(View callview) {
                Log.d(LOG_TAG, "## onViewLoading():");
            }

            @Override
            public void onViewReady() {
                Log.d(LOG_TAG, "## onViewReady(): ");

                if (null != mMxCall) {
                    if (mMxCall.isIncoming()) {
                        mMxCall.launchIncomingCall(null);
                        Log.d(LOG_TAG, "## onViewReady(): Incoming Call Launched ");
                    } else {
                        Log.d(LOG_TAG, "## onViewReady(): not incoming call");
                        //  thread.interrupt();
                    }
                }
            }

            /**
             * The call was answered on another device
             */
            @Override
            public void onCallAnsweredElsewhere() {
                InComingCallActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(LOG_TAG, "## onCallAnsweredElsewhere(): finish activity");
                        CommonActivityUtils.displayToastOnUiThread(InComingCallActivity.this, InComingCallActivity.this.getString(R.string.call_error_answered_elsewhere));
                        InComingCallActivity.this.finish();
                    }
                });
            }

            @Override
            public void onCallEnd(final int aReasonId) {
                InComingCallActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(LOG_TAG, "## onCallEnd(): finish activity");
                        CommonActivityUtils.processEndCallInfo(InComingCallActivity.this, aReasonId);
                        // thread.interrupt();
                        InComingCallActivity.this.finish();
                    }
                });
            }

            @Override
            public void onPreviewSizeChanged(int i, int i1) {

            }
        };


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setFinishOnTouchOutside(false);

            setContentView(R.layout.vector_incoming_call_dialog);

            Log.e(LOG_TAG, "## onCreate(): intent created");
            // retrieve intent extras
            final Intent intent = getIntent();
            if (intent == null) {
                Log.e(LOG_TAG, "## onCreate(): intent missing");
                finish();
            } else {
                mMatrixId = intent.getStringExtra(VectorCallViewActivity.EXTRA_MATRIX_ID);
                mCallId = intent.getStringExtra(VectorCallViewActivity.EXTRA_CALL_ID);

                if (null == mMatrixId) {
                    Log.e(LOG_TAG, "## onCreate(): matrix ID is missing in extras");
                    finish();
                } else if (null == mCallId) {
                    Log.e(LOG_TAG, "## onCreate(): call ID is missing in extras");
                    finish();
                } else if (null == (mSession = Matrix.getInstance(getApplicationContext()).getSession(mMatrixId))) {
                    Log.e(LOG_TAG, "## onCreate(): invalid session (null)");
                    finish();
                } else if (null == (mMxCall = mSession.mCallsManager.getCallWithCallId(mCallId))) {
                    Log.e(LOG_TAG, "## onCreate(): invalid call ID (null)");
                    finish();
                } else {

                    synchronized (LOG_TAG) {
                        if (null != sharedInstance) {
                            sharedInstance.finish();
                            Log.e(LOG_TAG, "## onCreate(): kill previous instance");
                        }

                        sharedInstance = this;
                    }

                    // UI widgets binding
                    mCallingUserAvatarView = (ImageView) findViewById(R.id.avatar_img);
                    mRoomNameTextView = (TextView) findViewById(R.id.room_name);
                    mAcceptCallButton = (Button) findViewById(R.id.button_incoming_call_accept);
                    mIgnoreCallButton = (Button) findViewById(R.id.button_incoming_call_ignore);

                    // set the avatar
                    VectorUtils.loadRoomAvatar(this, mSession, mCallingUserAvatarView, mMxCall.getRoom());

                    // set the room display name
                    String roomDisplayName = VectorUtils.getRoomDisplayName(this, mSession, mMxCall.getRoom());
                    if (null != roomDisplayName) {
                        mRoomNameTextView.setText(roomDisplayName);
                    } else {
                        mRoomNameTextView.setText("");
                    }

                    // set button handlers
                    mIgnoreCallButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onHangUp();
                            finish();
                        }
                    });

                    mAcceptCallButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startCallViewActivity(intent);
                            finish();
                        }
                    });

                    // create the call view to enable mMxCallListener being used,
                    // otherwise call API is not enabled
                    mMxCall.createCallView();
                }
            }


          /*  thread = new Thread() {
                @Override
                public void run() {
                    try {
                        while (!isInterrupted()) {
                            Thread.sleep(500);
                            if (hasWindowFocus() || iscalllcame) {
                                iscalllcame = true;
                                Log.d(LOG_TAG, "## Thread Running : OnResume Falied to call ");
                            } else {
                                //  InComingCallActivity.this.onResume();
                                final Intent intent = new Intent(InComingCallActivity.this, InComingCallActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.putExtra(VectorCallViewActivity.EXTRA_MATRIX_ID, mMatrixId);
                                intent.putExtra(VectorCallViewActivity.EXTRA_CALL_ID, mCallId);
                                startActivity(intent);
                                thread.interrupt();
                                Log.d(LOG_TAG, "## Thread Running : OnResume Called");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };*/
            // thread.start();

        }


        @Override
        public void finish() {
            super.finish();
            synchronized (LOG_TAG) {
                if (this == sharedInstance) {
                    sharedInstance = null;
                }
            }
        }
        @Override
        protected void onResume() {
            super.onResume();

            iscalllcame = false;
            mMxCall = mSession.mCallsManager.getCallWithCallId(mCallId);
            Log.d(LOG_TAG, "## onResume : here");
            if (null != mMxCall) {
                String callState = mMxCall.getCallState();
                boolean isWaitingUserResponse =
                        TextUtils.equals(callState, IMXCall.CALL_STATE_CREATING_CALL_VIEW) ||
                                TextUtils.equals(callState, IMXCall.CALL_STATE_FLEDGLING) ||
                                TextUtils.equals(callState, IMXCall.CALL_STATE_WAIT_LOCAL_MEDIA) ||
                                TextUtils.equals(callState, IMXCall.CALL_STATE_WAIT_CREATE_OFFER) ||
                                TextUtils.equals(callState, IMXCall.CALL_STATE_RINGING);
                if (isWaitingUserResponse) {
                    mMxCall.onResume();
                    mMxCall.addListener(mMxCallListener);
                } else {
                    Log.d(LOG_TAG, "## onResume : the call has already been managed.");
                    finish();
                }
            } else {
                Log.d(LOG_TAG, "## onResume : the call does not exist anymore");
                finish();
            }
        }

        @Override
        protected void onPause() {
            super.onPause();
            Log.d(LOG_TAG, "## onPause : here");
            if (null != mMxCall) {
                mMxCall.onPause();
                mMxCall.removeListener(mMxCallListener);
                Log.d(LOG_TAG, "## onPause : There");
            }
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.d(LOG_TAG, "## onDestory : There");
            if (null != mMxCall) {
                mMxCall.removeListener(mMxCallListener);
                Log.d(LOG_TAG, "## onDestory : here");
            }
        }

        @Override
        public void onBackPressed() {
            onHangUp();
        }

        /**
         * Helper method: starts the CallViewActivity in auto accept mode.
         * The extras provided in  are copied to
         * the CallViewActivity and {@link VectorCallViewActivity#EXTRA_AUTO_ACCEPT} is set to true.
         *
         * @param aSourceIntent the intent whose extras are transmitted
         */
        private void startCallViewActivity(final Intent aSourceIntent) {
            Intent intent = new Intent(this, VectorCallViewActivity.class);
            Bundle receivedData = aSourceIntent.getExtras();
            intent.putExtras(receivedData);
            intent.putExtra(VectorCallViewActivity.EXTRA_AUTO_ACCEPT, true);
            startActivity(intent);
        }

        /**
         * Hangup the call.
         */
        private void onHangUp() {
            if (null != mMxCall) {
                mMxCall.hangup("");
            }
        }

    }
4

0 回答 0