使用AsyncTask调用另一个应用程序
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人指出的那样,服务是最佳选择。但是,如果您使用 bindService() 服务只有在绑定时才会持续。
只是为了澄清一些问题:
startActivity
与后台任务进行通信并不是您想要的,因为 Activity 是一个用户界面组件,当激活时它将来到前台。您可以在没有 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:
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.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.
您是否考虑过将 com.test.messages.MessageCheck 设为 Service 而不是 Activity,然后调用 startService(...)
Have you considered make com.test.messages.MessageCheck a Service not an Activity, then calling startService(...)
您正在 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.