Android-从另一个活动中的 asyncTask postExecute 重新启动当前活动中的进程

发布于 2024-12-17 07:01:58 字数 987 浏览 0 评论 0原文

我有一个活动,允许用户将照片上传到网络,而不是让他们无限期地等待图像上传,而是在处理图像上传的异步任务中完成。当用户按下按钮上传图像时,我关闭了上传图像的活动,并且应用程序返回到具有自己的下载过程的早期活动。我想弄清楚的是在 asynctask onPostExecute 中做什么才能让其他活动中的下载过程再次运行。我尝试只使用这个:

  @Override
  protected void onPostExecute(String result){
    if(result!=null){
    Main.DownloadManager.startDownloading();
      }else if(result==null){
       Toast.makeText(getBaseContext(),"Upload failed",timetoshow).show();
              }
          }

但我在主要活动中没有得到任何响应,因为我知道我实际上并没有调用正在运行的活动,而只是调用该方法......在未实例化或运行的地方......或者至少就是这样我认为确实如此。所以我想我的问题是如何传递消息以从当前运行的 Main 活动中已关闭活动的 asynctask 运行 startDownloading() 方法。任何帮助将非常感激。

编辑:

我能够通过实施以下解决方案之一来解决这个问题:

 @Override
  protected void onPostExecute(String result){
    if(result!=null){
    CustomContextClass.*Main*.DownloadManager.startDownloading();
      }else if(result==null){
       Toast.makeText(getBaseContext(),"Upload failed",timetoshow).show();
              }
          }

I have an activity that lets a user upload a photo to the web and instead of having them wait indefinitely for the image to upload I have it being done in an asynctask that handles the image upload. When the user presses the button to upload the image the I have the activity that uploads the image close and the app goes back to the earlier activity which has its own downloading process. What Im trying to figure out is what to do in the asynctask onPostExecute to get the download process in the other activity to run again. I tried just using this:

  @Override
  protected void onPostExecute(String result){
    if(result!=null){
    Main.DownloadManager.startDownloading();
      }else if(result==null){
       Toast.makeText(getBaseContext(),"Upload failed",timetoshow).show();
              }
          }

But I get no response in the main activity because I know Im not actually calling the running activity but just calling the method.....somewhere thats not instantiated or running....or at least thats what I think this does. So I guess my question is how can I pass the message to run the startDownloading() method from the asynctask of the closed activity in the currently running Main activity. Any help would be super much appreciated.

EDIT:

I was able to solve this by implementing one of the solutions bellow:

 @Override
  protected void onPostExecute(String result){
    if(result!=null){
    CustomContextClass.*Main*.DownloadManager.startDownloading();
      }else if(result==null){
       Toast.makeText(getBaseContext(),"Upload failed",timetoshow).show();
              }
          }

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

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

发布评论

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

评论(4

橙幽之幻 2024-12-24 07:01:58

您可以在 DownloadManager 中使用静态方法

private static instance;

public void onCreate() {
    instance = this;
}

public static void startDownloading() {
    instance.downloadNow();
}

You may use a static method in DownloadManager

private static instance;

public void onCreate() {
    instance = this;
}

public static void startDownloading() {
    instance.downloadNow();
}
满天都是小星星 2024-12-24 07:01:58

AnsycTask 的 onProgressUpdate() 会告诉您进度。因此您可以在这里决定要发送到下一个活动的数据。在下一个活动中处理剩余的未下载部分。认为取决于数据块的服务器端实现。

需要您完整的要求才能获得更多建议。

onProgressUpdate() of AnsycTask will tell you about the progress .so here you can decide hewhat data you want to send to next activity . in next activity deal with remaining not downloaded portion . thinks depends on server side implementation of data chunk .

your complete requirement will be required for more suggestions.

感情废物 2024-12-24 07:01:58

让进程在死掉的 Activity 中挂起是很危险的,因为它很可能导致泄漏。为什么不在您想要显示进度的 Activity 中启动 AsyncTask 呢?这才是最有意义的。

Having processes hanging around from a dead Activity is dangerous, as it has a high potential to cause leaks. Why not start the AsyncTask in the Activity you intend to show progress? That would make the most sense.

趁年轻赶紧闹 2024-12-24 07:01:58

你可以做如下的事情,如果我没记错的话,我认为 AsyncTask 已经编写了 DownloadManager 类。
我希望您在该类中将 AsyncTask 编写为与此类似的 InnerClass。

private class InitTask extends AsyncTask<String, String, String> {....

另外你应该有一个 getter 来返回 InitTask 对象,

public InitTask getInitTask(){
    return new InitTask();
}

所以在另一个类中,你可以像下面这样调用它,

new YourOtherClass().getInitTask().execute();

you can do something like below, if I'm not mistaken I think the AsyncTask has written DownloadManager class.
I hope you have the AsyncTask written as a InnerClass similar to this in that class.

private class InitTask extends AsyncTask<String, String, String> {....

Also you should have to have a getter to return InitTask object,

public InitTask getInitTask(){
    return new InitTask();
}

So in the other class, you can call this like below,

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