简体   繁体   English

从其他类的Android调用Asynctask

[英]Call Asynctask from different class Android

I got class calledA and my async class is written as a inner class of A. I got another class called B(Activity). 我得到了一个叫做A的类,我的异步类是作为A的一个内部类编写的。我得到了另一个叫做B(Activity)的类。 Now im in B class. 现在我在B级。 what i want is to call asyncTask of A. 我想要的是调用A的asyncTask。

I'm much confused with this question & if anyone can give me a correct answer he'll be a life saver. 我对这个问题非常困惑,如果有人能给我正确答案,他将成为救生员。 Hope u guys help me. 希望你们能帮助我。 Thanks. 谢谢。

Its so simple just Simply build an object of main class and than call the inner class like this 它是如此简单,只需构建主类的对象,然后像这样调用内部类

 OuterMainClass outer = new OuterMainClass();
       outer.new InnerAsyncClass(param)
         .execute();

this answer is too late to help you but hope it help others. 这个答案来不及帮助您,但希望对其他人有所帮助。

Thanks 谢谢

you will need to move the async task from being annonymus class to be a normal class, instead of being inside class A. 您将需要将异步任务从匿名类移到普通类,而不是在A类内部。

Make a class that extends Asynctask and have two instances of that async task both in your activities. 创建一个扩展Asynctask的类,并在您的活动中同时具有该异步任务的两个实例。

Making a separate Class of AsyncTask . 制作一个单独的AsyncTask类。 and calling it in both activities is the answer. 并在两种activities都将其称为答案。

您可以在A类中将Async Task声明为公共静态内部类,然后从B类执行它。

I was in the same situation! 我当时处于同样的情况! Look at this answer...make on one service class to handle all your threading....perfect example here: 看看这个答案...在一个服务类上处理所有线程....完美的示例在这里:

Good Example on GitHub WebServiceAsyncTask and WebServiceBackgroundActivity . GitHub WebServiceAsyncTaskWebServiceBackgroundActivity的好例子。 VIA Android: How to run asynctask from different class file? 威盛Android:如何从其他类文件运行asynctask?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM