同步 startActivityForResult - 等待 Activity 完成

发布于 2024-11-15 14:29:43 字数 739 浏览 7 评论 0原文

我有一个正在启动新活动的应用程序,并且需要在继续之前获得该活动的结果。

我意识到 startActivityForResult 是异步/非阻塞的,并且我可以在 onActivityResult 回调中获取活动的结果。

所以我想我正在寻找的是等待活动返回的最佳方式...... 也许是这样的?或者有更好的方法吗?

Activity Launcher 函数:

public String ActivityLauncher()
{
   //Set up Intent
   startActivityForResult(intent, 1);
   while (mIsActivityDone == false)
   {
       Thread.Sleep(250);
   }
   //Continue with processing
   String data = "<Data from Activity">
   return data;
}

回调:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
   //Pull out the data
   mIsActivityDone = true;
}

数据需要返回到更高级别的调用函数 - 这就是为什么我需要在 ActivityLauncher 函数中等待结果。

谢谢!

I have an application where I am launching a new Activity, and need to have the result of the activity before proceeding.

I realize that startActivityForResult is asynchronous / non-blocking, and that I can get the result of the activity in the onActivityResult callback.

So I guess what I'm looking for is the best way to wait for the activity to return...
Something like this perhaps? Or is there a better way?

Activity Launcher Function:

public String ActivityLauncher()
{
   //Set up Intent
   startActivityForResult(intent, 1);
   while (mIsActivityDone == false)
   {
       Thread.Sleep(250);
   }
   //Continue with processing
   String data = "<Data from Activity">
   return data;
}

Callback:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
   //Pull out the data
   mIsActivityDone = true;
}

The data needs to be returned to a higher level calling function - this is why I need to wait for the result in the ActivityLauncher function.

Thanks!

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

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

发布评论

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

评论(2

棒棒糖 2024-11-22 14:29:43

这将阻塞您的 UI 线程,因此其他活动也不会执行。
在这种情况下,我会使用更高级别函数提供的某种回调类,并在 onActivityResult 中调用其回调函数

This will block your UI thread so other activity will not execute either.
I would use some kind of callback class provided by higher level function, in this kind of situation and call its callback function in onActivityResult

不必你懂 2024-11-22 14:29:43

如果您可以从新线程启动这些活动,那么 Android 将为您提供解决方案。您可以使用仪器。为什么需要新线程?因为 startActivitySync 会检查它是否在主线程中被调用。如果它是主线程那么它会抛出异常。

仪器可让您监控活动。您可以以阻塞或非阻塞方式监视此活动。

IntentFilter intF = new IntentFilter("ACTIVITY.THAT.YOU.WANT.TO.LAUNCH");

Instrumentation instrumentation = new Instrumentation();

Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(intF, null, true);
Intent i = new Intent("ACTIVITY.THAT.YOU.WANT.TO.LAUNCH");
instrumentation.startActivitySync(i);

If you can start these activities from a new thread then Android has a solution for you. You can use Instrumentation. Why do you need a new thread? Because startActivitySync will check if you its being invoked in a main thread. If its main thread then it will throw an exception.

Instrumentation lets you monitor an activity. You can monitor this activity in either blocking or non blocking fashion.

IntentFilter intF = new IntentFilter("ACTIVITY.THAT.YOU.WANT.TO.LAUNCH");

Instrumentation instrumentation = new Instrumentation();

Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(intF, null, true);
Intent i = new Intent("ACTIVITY.THAT.YOU.WANT.TO.LAUNCH");
instrumentation.startActivitySync(i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文