如何进行阻塞和同步活动?

发布于 2024-11-04 10:08:00 字数 1539 浏览 1 评论 0原文

好吧,请不要问我为什么,但我需要启动一个同步和阻塞活动,以便程序流程在完成之前不会继续。我知道如何进行同步对话框,但是如何进行同步活动?

以下是我尝试过但失败的两种方法:

// In the 1st activity, start the 2nd activity in the usual way
startActivity(intent);
Looper.loop();        // but pause the program here
// Program continuse running afer Looper.loop() returns
....

// Then, in the second activity's onBackPressed method:
public void onBackPressed() {
    // I was hoping the following quit() will terminate the loop() call in
    // the first activity. But it caused an exception "Main thread not allowed
    // to quit." Understandable.
    new Hanlder().getLooper().quit();
}

我还尝试使用另一个线程来实现此目的:

// In the first activity, start a thread
SecondThread m2ndThread = new SecondThread(this);
Thread th = new Thread(m2ndThread, "Second Thread");
th.run();
synchronized(m2ndThread) {
    m2ndThread.wait();
}

class SecondThread implements Runnable {

    Activity m1stActivity;

    SecondThread(Activity a) {
        m1stActivity = a;
    }

    public void run() {
        Looper.prepare();
        Handler h = new Handler() {
            public void handleMessage() {
                Intent intent = new Intent(m1stActivity, SecondActivity.class);
                m1stActivity.startActivity(intent); // !!!!!!!!!!!!!!
            }
        }
        h.sendEmptyMessage(10); // let it run
        Looper.quit();
    }
}

但是,这种方法不起作用,因为当我使用第一个活动启动第二个活动时,主线程已经处于等待状态状态并且什么也不做。所以第二个活动甚至没有被创建。 (这很讽刺:你必须使用一个活动来启动一个活动,但是当它已经处于等待状态时你怎么能做到这一点呢?)

Well, please don't ask me why, but I need to start a synchronous and blocking activity so that the program flow won't continue until it's finished. I know how to do a synchronous Dialog, but how can I do a synchronous activity?

Following are two approaches I tried but failed:

// In the 1st activity, start the 2nd activity in the usual way
startActivity(intent);
Looper.loop();        // but pause the program here
// Program continuse running afer Looper.loop() returns
....

// Then, in the second activity's onBackPressed method:
public void onBackPressed() {
    // I was hoping the following quit() will terminate the loop() call in
    // the first activity. But it caused an exception "Main thread not allowed
    // to quit." Understandable.
    new Hanlder().getLooper().quit();
}

I also tried to use another thread to achieve this:

// In the first activity, start a thread
SecondThread m2ndThread = new SecondThread(this);
Thread th = new Thread(m2ndThread, "Second Thread");
th.run();
synchronized(m2ndThread) {
    m2ndThread.wait();
}

class SecondThread implements Runnable {

    Activity m1stActivity;

    SecondThread(Activity a) {
        m1stActivity = a;
    }

    public void run() {
        Looper.prepare();
        Handler h = new Handler() {
            public void handleMessage() {
                Intent intent = new Intent(m1stActivity, SecondActivity.class);
                m1stActivity.startActivity(intent); // !!!!!!!!!!!!!!
            }
        }
        h.sendEmptyMessage(10); // let it run
        Looper.quit();
    }
}

However, this approach doesn't work because when I'm using the 1st activity to start the 2nd activity, the main thread is already in wait state and doing nothing. So the 2nd activity didn't even get created. (This is ironical: You have to use an activity to start an activity, but how can you do that when it's already in wait state?)

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

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

发布评论

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

评论(2

深海不蓝 2024-11-11 10:08:00

你不能。

这就是全部内容了。你就是不能这样做。活动始终在主线程上执行。您的主线程必须主动运行其事件循环。阻塞主线程的事件循环总是会被破坏。总是。

You can't.

That is all there is to it. You just can't do this. Activities execute on the main thread, always. Your main thread must be actively running its event loop. It is ALWAYS broken to block the main thread's event loop. Always.

只是偏爱你 2024-11-11 10:08:00

所有活动都是异步的。但是,如果您想阻止应用程序流程的其余部分直到活动完成,您可以注入对活动设置的标志的依赖项。最封装的方法是将标志与意图/活动/返回流一起携带。您可以:

在该对象的全局作用域中声明该标志:

boolean syncBlock = false;
int ASYNC_ACTIVITY_REQUEST_CODE = 1; // Arbitrary unique int

在启动新活动的对象作用域中包括: 在

Intent asyncIntent = new Intent(this, AsyncActivity.class);
syncBlock = true;
startActivityForResult(asyncIntent, ASYNC_ACTIVITY_REQUEST_CODE);
while(syncBlock) {}

启动该活动的对象中:

onActivityResult(int requestCode, int resultCode, Intent data)
{
  switch(requestCode)
  {
    case ASYNC_ACTIVITY_REQUEST_CODE:
    {
      syncBlock = false;
      break;
    }

[...]
}

这是一个粗略的黑客行为,如果您在 UI 线程中阻塞(例如. 在您的 MAIN 活动中)您阻止了所有内容,包括您的用户可能期望响应但不会响应的其他 UI 功能。这是一个很大的禁忌,您应该学习使用异步流程,使应用程序以 Android 方式运行。但如果你绝对必须...

All activities are asynchronous. But if you want to block the rest of your application flow until the activity finishes, you can inject a dependency on a flag that the activity sets. The most encapsulated way to do so would be to carry the flag along with the intent/activity/return flow. You could:

Declare the flag in that object's global scope:

boolean syncBlock = false;
int ASYNC_ACTIVITY_REQUEST_CODE = 1; // Arbitrary unique int

In the object's scope that starts the new activity include:

Intent asyncIntent = new Intent(this, AsyncActivity.class);
syncBlock = true;
startActivityForResult(asyncIntent, ASYNC_ACTIVITY_REQUEST_CODE);
while(syncBlock) {}

And in in the object that started the activity:

onActivityResult(int requestCode, int resultCode, Intent data)
{
  switch(requestCode)
  {
    case ASYNC_ACTIVITY_REQUEST_CODE:
    {
      syncBlock = false;
      break;
    }

[...]
}

It's a crude hack, and if you're blocking in your UI thread (eg. in your MAIN activity) you're blocking everything, including other UI features your users will probably expect to respond but won't. It's a big nono, and you should learn to go with the async flow that makes apps work the Android way. But if you absolutely must...

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