如何在服务类中调用supportFragmentManger?

时间:2016-05-15 11:15:40

标签: android android-dialogfragment

我创建了一个服务类来处理我的应用的位置。但我有一个错误对话框片段,所以如何调用supportFragmentManger?我还在服务类中为对话框片段创建了一个类。

private void showErrorDialog(int errorCode) {
        // Get the error dialog from Google Play services
        Dialog errorDialog =
                GooglePlayServicesUtil.getErrorDialog(errorCode, (Activity) mContext,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);

        // If Google Play services can provide an error dialog
        if (errorDialog != null) {

            // Create a new DialogFragment in which to show the error dialog
            ErrorDialogFragment errorFragment = new ErrorDialogFragment();

            // Set the dialog in the DialogFragment
            errorFragment.setDialog(errorDialog);

            // Show the error dialog in the DialogFragment
            errorFragment.show(getSupportFragmentManager(), "Wyntr-Beta");
        }
    }

    public static class ErrorDialogFragment extends DialogFragment {
        // Global field to contain the error dialog
        private Dialog mDialog;

        /**
         * Default constructor. Sets the dialog field to null
         */
        public ErrorDialogFragment() {
            super();
            mDialog = null;
        }

        /*
         * Set the dialog to display
         *
         * @param dialog An error dialog
         */
        public void setDialog(Dialog dialog) {
            mDialog = dialog;
        }

        /*
         * This method must return a Dialog to the DialogFragment.
         */
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return mDialog;
        }
    }

1 个答案:

答案 0 :(得分:1)

  

但是我有一个错误对话框片段,所以如何调用supportFragmentManger?

在启动服务之前,将所有此代码移动到您的活动中。服务无法显示片段。在开始从服务或其他非活动上下文中使用其API之前,您需要验证Play服务是否可以使用。

相关问题