如何使用FragmentManager替代pcated的ShowDialog去$ P $ [英] How to use FragmentManager to substitute the deprecated showDialog

查看:250
本文介绍了如何使用FragmentManager替代pcated的ShowDialog去$ P $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的方法displayDialog ......它的工作原理,但Eclipse的表明我对这个警告:

 的ShowDialog(showRetry 1:0);

方法:

 私人无效displayDialog(最终布尔showRetry){
                    mHandler.post(新的Runnable(){
                        公共无效的run(){
                            setProgressBarIndeterminateVisibility(假);
                            的ShowDialog(showRetry?1:0);
                        }
                    });
                }

搜索我也发现了类似的问题在哪见过,现在就有的 FragmentManager 的,可以用来避免这个问题,但我不明白我怎么可以用它来修复这个问题我具体情况维持与升级Froyo和姜饼复古的兼容性。

你能帮助我吗?

完整的类code是:

 公共类家庭延伸活动{
        私有静态最后弦乐BASE64_PUBLIC_KEY =mykeywithoutspaces;        //生成自己的20随机字节,并把它们放在这里。
        私有静态最后一个字节[] = SALT新的字节[] {
            -00,00,30,-2,-58,-57,00,-64,51,32,-95,-45,13 -6,-36,-15,-11,32,-64,
            89
        };        私人TextView的mStatusText;
        私人按钮mCheckLicenseButton;        私人LicenseCheckerCallback mLicenseCheckerCallback;
        私人LicenseChecker mChecker;
        // UI线程的处理程序。
        私人处理器mHandler;        @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
            的setContentView(R.layout.main);            mStatusText =(的TextView)findViewById(R.id.status_text);
buttonNewDoc =(的ImageButton)findViewById(R.id.btNewDoc);
        buttonNewDoc.setOnClickListener(mNewDoc);
            mCheckLicenseButton =(按钮)findViewById(R.id.check_license_button);
            mCheckLicenseButton.setOnClickListener(新View.OnClickListener(){
                公共无效的onClick(查看视图){
                    doCheck();
                }
            });            mHandler =新的处理程序();            //尝试在这里使用更多的数据。 ANDROID_ID是攻击的一个点。
            字符串的DeviceID = Secure.getString(getContentResolver(),Secure.ANDROID_ID);            当它完成//库调用此。
            mLicenseCheckerCallback =新MyLicenseCheckerCallback();
            //构造一个政策LicenseChecker。
            mChecker =新LicenseChecker(
                对此,新ServerManagedPolicy(这一点,
                    新AESObfuscator(SALT,getPackageName(),DEVICEID))
                BASE64_PUBLIC_KEY);
            doCheck();
        }        保护对话框onCreateDialog(INT ID){
            最终布尔bRetry = ID = = 1;
            返回新AlertDialog.Builder(本)
                .setTitle(R.string.unlicensed_dialog_title)
                .setMessage(?bRetry R.string.unlicensed_dialog_retry_body:R.string.unlicensed_dialog_body)
                .setPositiveButton(bRetry R.string.retry_button:R.string.buy_button,新DialogInterface.OnClickListener(){
                    布尔mRetry = bRetry;
                    公共无效的onClick(DialogInterface对话,诠释它){
                        如果(mRetry){
                            doCheck();
                        }其他{
                            意图marketIntent =新意图(Intent.ACTION_VIEW,Uri.parse(
                                    http://market.android.com/details?id=+ getPackageName()));
                                startActivity(marketIntent);
                        }
                    }
                })
                .setNegativeButton(R.string.quit_button,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释它){
                        完();
                    }
                })。创建();
        }        私人无效doCheck(){
buttonNewDoc.setEnabled(假);
            mCheckLicenseButton.setEnabled(假);
            setProgressBarIndeterminateVisibility(真);
            mStatusText.setText(R.string.checking_license);
            mChecker.checkAccess(mLicenseCheckerCallback);
        }        私人无效displayResult(最后弦乐结果){
            mHandler.post(新的Runnable(){
                公共无效的run(){
                    mStatusText.setText(结果);
                    setProgressBarIndeterminateVisibility(假);
                    mCheckLicenseButton.setEnabled(真);
                }
            });
        }        私人无效displayDialog(最终布尔showRetry){
            mHandler.post(新的Runnable(){
                公共无效的run(){
                    setProgressBarIndeterminateVisibility(假);
                    的ShowDialog(showRetry?1:0);
                }
            });
        }        私有类MyLicenseCheckerCallback实现LicenseCheckerCallback {
            公共无效允许(INT policyReason){
                如果(isFinishing()){
                    //如果活动是整理不更新UI。
                    返回;
                }                //应该允许用户访问。
                displayResult(的getString(R.string.allow));
buttonNewDoc.setEnabled(假);
            }            公共无效dontAllow(INT policyReason){
                如果(isFinishing()){
                    //如果活动是整理不更新UI。
                    返回;
                }
                displayResult(的getString(R.string.dont_allow));
                //不应该允许访问。在大多数情况下,应用程序应该承担
                //用户具有访问权限,除非遇到这一点。如果是这样,
                //应用程序应该通知自己的无牌方式用户
                //然后要么关闭应用程序或用户限制到
                //限制的功能集。
                //在这个例子中,我们显示一个对话框,将用户带到市场。
                //如果缺少许可证的原因在于,该服务是
                //不可用或有另外一个问题,我们显示一个
                //重试对话框,不同的消息按钮。
                displayDialog(policyReason == Policy.RETRY);
            }            公共无效的ApplicationError(INT错误code){
                如果(isFinishing()){
                    //如果活动是整理不更新UI。
                    返回;
                }
                字符串结果=的String.format(的getString(R.string.application_error),错误code);
                displayResult(结果);
            }
        }        @覆盖
        保护无效的onDestroy(){
            super.onDestroy();
            mChecker.onDestroy();
        }    }


解决方案

您的活动应该继承 FragmentActivity ,可在的 V4支持库,你应该添加到您的项目。然后,您可以获取到 FragmentManager 使用 getSupportFragmentManager()来显示 DialogFragment <参考/ code>。你必须创建一个 DialogFragment 的布局和你的对话需要逻辑类。下面是你期望从它显示你的 FragmentActivity

  FragmentManager FM = getSupportFragmentManager();
YourDialogFragment yourDialog =新YourDialogFragment();
yourDialog.show(FM,some_optional_t​​ag);

下面是一个简要介绍: HTTP://android-developers.blogspot。 COM / 2012/05 /使用-dialogfragments.html

I am using the method displayDialog... it works but Eclipse shows me a warning on this:

showDialog(showRetry ? 1 : 0);

Method:

   private void displayDialog(final boolean showRetry) {
                    mHandler.post(new Runnable() {
                        public void run() {
                            setProgressBarIndeterminateVisibility(false);
                            showDialog(showRetry ? 1 : 0);
                        }
                    });
                } 

Searching I have found similar question in which seen that now there is the FragmentManager that can be used to avoid the problem, but I haven't understand how I could use to fix the problem in my specific scenario maintaining retro-compatibility with Froyo and GingerBread.

Could you help me?

The full class code is:

 public class Home extends Activity {
        private static final String BASE64_PUBLIC_KEY = "mykeywithoutspaces";

        // Generate your own 20 random bytes, and put them here.
        private static final byte[] SALT = new byte[] {
            -00, 00, 30, -2, -58, -57, 00, -64, 51, 32, -95, -45, 13, -6, -36, -15, -11, 32, -64,
            89
        };

        private TextView mStatusText;
        private Button mCheckLicenseButton;

        private LicenseCheckerCallback mLicenseCheckerCallback;
        private LicenseChecker mChecker;
        // A handler on the UI thread.
        private Handler mHandler;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
            setContentView(R.layout.main);

            mStatusText = (TextView) findViewById(R.id.status_text);
buttonNewDoc = (ImageButton) findViewById(R.id.btNewDoc);
        buttonNewDoc.setOnClickListener(mNewDoc);
            mCheckLicenseButton = (Button) findViewById(R.id.check_license_button);
            mCheckLicenseButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    doCheck();
                }
            });

            mHandler = new Handler();

            // Try to use more data here. ANDROID_ID is a single point of attack.
            String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

            // Library calls this when it's done.
            mLicenseCheckerCallback = new MyLicenseCheckerCallback();
            // Construct the LicenseChecker with a policy.
            mChecker = new LicenseChecker(
                this, new ServerManagedPolicy(this,
                    new AESObfuscator(SALT, getPackageName(), deviceId)),
                BASE64_PUBLIC_KEY);
            doCheck();
        }

        protected Dialog onCreateDialog(int id) {
            final boolean bRetry = id == 1;
            return new AlertDialog.Builder(this)
                .setTitle(R.string.unlicensed_dialog_title)
                .setMessage(bRetry ? R.string.unlicensed_dialog_retry_body : R.string.unlicensed_dialog_body)
                .setPositiveButton(bRetry ? R.string.retry_button : R.string.buy_button, new DialogInterface.OnClickListener() {
                    boolean mRetry = bRetry;
                    public void onClick(DialogInterface dialog, int which) {
                        if ( mRetry ) {
                            doCheck();
                        } else {
                            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
                                    "http://market.android.com/details?id=" + getPackageName()));
                                startActivity(marketIntent);                        
                        }
                    }
                })
                .setNegativeButton(R.string.quit_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).create();
        }

        private void doCheck() {
buttonNewDoc.setEnabled(false);
            mCheckLicenseButton.setEnabled(false);
            setProgressBarIndeterminateVisibility(true);
            mStatusText.setText(R.string.checking_license);
            mChecker.checkAccess(mLicenseCheckerCallback);
        }

        private void displayResult(final String result) {
            mHandler.post(new Runnable() {
                public void run() {
                    mStatusText.setText(result);
                    setProgressBarIndeterminateVisibility(false);
                    mCheckLicenseButton.setEnabled(true);
                }
            });
        }

        private void displayDialog(final boolean showRetry) {
            mHandler.post(new Runnable() {
                public void run() {
                    setProgressBarIndeterminateVisibility(false);
                    showDialog(showRetry ? 1 : 0);
                }
            });
        }    

        private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
            public void allow(int policyReason) {
                if (isFinishing()) {
                    // Don't update UI if Activity is finishing.
                    return;
                }

                // Should allow user access.
                displayResult(getString(R.string.allow));
buttonNewDoc.setEnabled(false);
            }

            public void dontAllow(int policyReason) {
                if (isFinishing()) {
                    // Don't update UI if Activity is finishing.
                    return;
                }
                displayResult(getString(R.string.dont_allow));
                // Should not allow access. In most cases, the app should assume
                // the user has access unless it encounters this. If it does,
                // the app should inform the user of their unlicensed ways
                // and then either shut down the app or limit the user to a
                // restricted set of features.
                // In this example, we show a dialog that takes the user to Market.
                // If the reason for the lack of license is that the service is
                // unavailable or there is another problem, we display a
                // retry button on the dialog and a different message.
                displayDialog(policyReason == Policy.RETRY);
            }

            public void applicationError(int errorCode) {
                if (isFinishing()) {
                    // Don't update UI if Activity is finishing.
                    return;
                }
                String result = String.format(getString(R.string.application_error), errorCode);
                displayResult(result);
            }
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            mChecker.onDestroy();
        }

    }

解决方案

Your activity should subclass FragmentActivity, which can be found in the v4 support library, which you should add to your project. You then can get a reference to the FragmentManager using getSupportFragmentManager() to show a DialogFragment. You'll have to create a DialogFragment class with the layout and the logic that your dialog requires. Here's how you'd show it from your FragmentActivity:

FragmentManager fm = getSupportFragmentManager();
YourDialogFragment yourDialog = new YourDialogFragment();
yourDialog.show(fm, "some_optional_tag");

Here's an overview: http://android-developers.blogspot.com/2012/05/using-dialogfragments.html

这篇关于如何使用FragmentManager替代pcated的ShowDialog去$ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆