Android-从另一个活动中的 asyncTask postExecute 重新启动当前活动中的进程
我有一个活动,允许用户将照片上传到网络,而不是让他们无限期地等待图像上传,而是在处理图像上传的异步任务中完成。当用户按下按钮上传图像时,我关闭了上传图像的活动,并且应用程序返回到具有自己的下载过程的早期活动。我想弄清楚的是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 DownloadManager 中使用静态方法
You may use a static method in DownloadManager
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.
让进程在死掉的 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.你可以做如下的事情,如果我没记错的话,我认为
AsyncTask
已经编写了DownloadManager
类。我希望您在该类中将
AsyncTask
编写为与此类似的 InnerClass。另外你应该有一个 getter 来返回 InitTask 对象,
所以在另一个类中,你可以像下面这样调用它,
you can do something like below, if I'm not mistaken I think the
AsyncTask
has writtenDownloadManager
class.I hope you have the
AsyncTask
written as a InnerClass similar to this in that class.Also you should have to have a getter to return InitTask object,
So in the other class, you can call this like below,