如何在 Android 中模拟同步获取选项?

发布于 2024-10-20 13:39:43 字数 1987 浏览 5 评论 0原文

好的,我正在尝试编写一个实时文件夹提供程序,在调用它和创建文件夹之间,首先会询问用户一些选项。它一直有效,直到我在创建文件夹之前尝试等待选项为止。我很确定这是由于我试图等待期权活动所致。

我正在尝试通过 1 槽信号量等待选项。这是我的基本半伪代码:

package mypackage

import <all-my-imports>

public class Folder extends Activity {
    Intent getOptions = null;

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

        final Intent intent = getIntent();
        final String action = intent.getAction();

        if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {           
            try {
                CoreLib.Blocker().acquire();                
                getOptions = new Intent(this, Options.class);
                startActivityForResult(getOptions, 0);              
                CoreLib.Blocker().acquire();

                if(getOptions == null) { 
                    throw new Exception("Live folder canceled."); 
                }

                <create-live-folder-from-options>

                setResult(RESULT_CANCELED);
                finish();
            } catch (Exception e) {
                CoreLib.showToast(e.getMessage(), Toast.LENGTH_LONG);

                setResult(RESULT_CANCELED);
                finish();
            }
        } else {
            setResult(RESULT_CANCELED);
            finish();
        }
    }

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        getOptions = intent;
        CoreLib.Blocker().release();
    }   
}

CoreLib.Blocker() 是一个信号量,初始化如下:new Semaphore(1, true)

我的想法是,它将获取信号量,然后启动选项活动,然后尝试再次获取信号量,这将锁定它,直到选项活动返回,然后调用释放> 在 onActivityResult 中,然后 onCreate 将继续。但它似乎不起作用......只是在第二次获取时挂断,仅此而已。

在创建文件夹之前从用户那里获取选项的最佳方法是什么,以便我可以在创建文件夹时使用这些选项?

更新:我现在也尝试用空的while循环替换我的整个小信号量逻辑调用选项活动后,等待共享布尔变量,选项活动完成后将被翻转。但这也会让应用程序在进入“选项”活动之前就被遗忘。

Okay, I am trying to write a Live Folder provider, which between being called and creating the folder, first asks the user for some options. It works until I get to the point of trying to wait for the options before creating the folder. I am pretty sure it's due to how I am trying to wait on the options activity.

I am attempting to wait on the options via a 1-slot semaphore. Here is my basic semi-pseudo code:

package mypackage

import <all-my-imports>

public class Folder extends Activity {
    Intent getOptions = null;

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

        final Intent intent = getIntent();
        final String action = intent.getAction();

        if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {           
            try {
                CoreLib.Blocker().acquire();                
                getOptions = new Intent(this, Options.class);
                startActivityForResult(getOptions, 0);              
                CoreLib.Blocker().acquire();

                if(getOptions == null) { 
                    throw new Exception("Live folder canceled."); 
                }

                <create-live-folder-from-options>

                setResult(RESULT_CANCELED);
                finish();
            } catch (Exception e) {
                CoreLib.showToast(e.getMessage(), Toast.LENGTH_LONG);

                setResult(RESULT_CANCELED);
                finish();
            }
        } else {
            setResult(RESULT_CANCELED);
            finish();
        }
    }

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        getOptions = intent;
        CoreLib.Blocker().release();
    }   
}

CoreLib.Blocker() is a semaphore which is initialized as so: new Semaphore(1, true).

My thinking was that it would acquire the semaphore, then launch the Options activity, it would then try to acquire the semaphore again, which would lock it up until the Options activity returned, where it would then call the release in the onActivityResult, and then the onCreate would continue. It seems to not work though... just hangs up on the second acquire and that is it.

What is the best way to go about getting options from the user, before creating the folder, so that I can use those options in the creation of the folder?

UPDATE: I've also now tried replacing my whole little semaphore logic with an empty while loop after calling the Options activity, waiting on a shared boolean variable which would be flipped by the Options activity when it was finished. But this also sends the app off into oblivion before it can ever get to the Options activity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫弦 2024-10-27 13:39:43

弄清楚了这一点...一旦我弄清楚了这一点,似乎相当愚蠢-愚蠢,但这里是...

而不是将我的选项选择放入单独的活动中,只需在该活动上放置一个布局,该布局将 LiveFolder 意图返回到回家,并在那里收集我的选择。

我以为您必须在 onCreate 覆盖中 setResult(RESULT_OK, createLiveFolder()) ,但没有意识到您可以将其延迟到单击按钮或列表。一旦我弄清楚了这一点,一切就水到渠成了。

Figured this out... seems rather stupid-silly obvious once I figured it out, but here it is...

Instead of putting my options choices into a separate activity, just put an a layout on the activity which returns the LiveFolder intent to home, and gather my options on there.

I thought you had to setResult(RESULT_OK, createLiveFolder()) in the onCreate override, didn't realize you could delay it until a button- or list-click. Once I figured that out, it all fell into place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文