使用AsyncTask调用另一个应用程序

发布于 2024-11-17 15:37:37 字数 972 浏览 0 评论 0原文

我想使用 AsyncTask 调用另一个应用程序。我有一个主应用程序,它可以做自己的事情,但是当我启动它时,它会执行一个 AsyncTask 来调用“消息”应用程序,该应用程序检查是否有任何未读消息,然后在状态栏中显示通知。

然而,当我调用execute()时,消息应用程序跳到前面,我必须按后退按钮才能到达我的主应用程序。

代码如下(我的主应用程序)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MessageUpdate update = new MessageUpdate(this);
    System.out.println("Calling Update");
    update.execute(null);
....

我的AsyncTask:

public class MessageUpdate extends AsyncTask{

    Context ctx;
    public MessageUpdate(Context ctx){
        this.ctx = ctx;
    }

    @Override
    protected Object doInBackground(Object... params) {
        Intent notificationIntent = new Intent();
        notificationIntent.setClassName("com.test.messages", "com.test.messages.MessageCheck");
        ctx.startActivity(notificationIntent);
        return null;
    }
}

谁能告诉我为什么MessagesCheck跳到前面以及如何停止它?或者我正在做的事情是否可行?

I want to use AsyncTask to call another application. I have a main app that does its own thing but when I launch it, it does an AsyncTask to call a "messages" application which checks to see if there are any unread messages and then displays a notification in the status bar.

However, when I called execute(), the messages application jumps to the front and I have to press the back button to get to my main application.

Code is as follows (my main application)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MessageUpdate update = new MessageUpdate(this);
    System.out.println("Calling Update");
    update.execute(null);
....

My AsyncTask:

public class MessageUpdate extends AsyncTask{

    Context ctx;
    public MessageUpdate(Context ctx){
        this.ctx = ctx;
    }

    @Override
    protected Object doInBackground(Object... params) {
        Intent notificationIntent = new Intent();
        notificationIntent.setClassName("com.test.messages", "com.test.messages.MessageCheck");
        ctx.startActivity(notificationIntent);
        return null;
    }
}

Can anyone tell my why the MessagesCheck jumps to the front and how to stop it? Or if what I'm doing is even feasible?

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

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

发布评论

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

评论(3

祁梦 2024-11-24 15:37:37

正如其他人指出的那样,服务是最佳选择。但是,如果您使用 bindService() 服务只有在绑定时才会持续。

只是为了澄清一些问题:

  • 使用 startActivity 与后台任务进行通信并不是您想要的,因为 Activity 是一个用户界面组件,当激活时它将来到前台。
  • 建议您向“消息”应用程序添加 服务不是主要应用程序。
  • 然后,您可以通过绑定到服务从主应用程序与“消息”应用程序进行通信。如果您这样做(使用bindService),该服务将不会无限期地运行。
  • 有关如何执行此操作的详细信息,请参见此处

您可以在没有 AsyncTask 的情况下完成所有这些操作,但是如果服务需要大量时间来完成其工作,它可能有助于避免阻塞 UI 线程。或者,您的服务可以使用自己的后台线程来检查消息并发布通知。

As others have pointed out, a service is the way to go. However, if you use bindService() the service will only last as long as it's bound.

Just to clarify some of the issues:

  • Using startActivity to communicate with a background task is not what you want, as an Activity is a user interface component, which will come to the front when activated.
  • The suggestion is that you add a service to your "messages" application, not the main application.
  • You can then communicate with the "messages" application from the main application by binding to the service. If you do this (using bindService), the service will not run indefinitely.
  • The details of how to do this are here.

You could do all this without an AsyncTask, however it may help to avoid blocking the UI thread if the service takes significant time to do its job. Alternatively, your service could use a background thread of its own to check for messages and post the notification.

青巷忧颜 2024-11-24 15:37:37

您是否考虑过将 com.test.messages.MessageCheck 设为 Service 而不是 Activity,然后调用 startService(...)

Have you considered make com.test.messages.MessageCheck a Service not an Activity, then calling startService(...)

吖咩 2024-11-24 15:37:37

您正在 ASyncTask 中启动一个 Activity,因此您当前的 Activity 会被带到后面。

为了防止这种情况,您必须使用不同的方式来检查是否有新消息。您可以在 ASyncTask 中进行验证并显示其中的通知,或者使用服务。

You are starting an Activity in your ASyncTask, so your current Activity is brought to the back.

In order to prevent that, you'll have to use a different way to check if there is a new message. You could either do that verification in your ASyncTask and display the notification from it, or use a Service.

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