如何一个接口,可用于不同的背景Android的任务是什么?接口、背景、不同、任务

2023-09-12 00:39:01 作者:愿风裁尘

嗯,我有两个后台任务(异步任务),其中有两个独立的类定义了像

Well, I have an activity class with two background task (Async-Task) which have been defined in two separate classes like

public class GettingBeaconsList extends AsyncTask<String, String, String> 
public class GettingAirports extends AsyncTask<String, String, String> 

这是初始化,并在执行 MainClass

public class MainClass extends Activity implements DelegateTaskCompleted
{

     int ServiceBoolean = 0;
     public OnClickListener LoadingBeaconList = new OnClickListener()
     {
         public void onClick(View v)
         {
             ServiceBoolean  =1;
             new GettingBeaconsList (context,MainClass.this).execute();
         }
     }

    public OnClickListener LoadingAirportList= new OnClickListener()
    {
         public void onClick(View v)
         {
             ServiceBoolean  =2;
             new GettingAirports(context,MainClass.this).execute();
         }
    }


    @Override
    public void JsonArrayLoaded(JSONArray result) 
    {
        // bla bla or whatever here i write, does not matter
         if(ServiceBoolean  == 1)   { 
                //  Load activity 5
         }

         else if( ServiceBoolean  == 2)
         { 
             //  Load activity 6

         }

        else if( ServiceBoolean==3 )
        {
            // display Toast or Alert Box or load Activity number 8
        } 


    }

}

现在在上面code MainClass.this存储为AsynTask子界面参考类似这样

Now in above code MainClass.this is stored as Interface Reference in AsynTask SubClasses like this

private Context context             =   null;
private DelegateTaskCompleted delegate      =   null;

public GettingBeaconsList (Context context,DelegateTaskCompleted delegate)  
{   
    this.context        =   context;
    this.delegate       =   delegate;
}

// And constructor of second AsynTask is same as of First AsynTask Class

private Context context             =   null;
private DelegateTaskCompleted delegate      =   null;

public GettingAirports (Context context,DelegateTaskCompleted delegate) 
{   
    this.context        =   context;
    this.delegate       =   delegate;
}

onPostExecute每个AsynTask类或子类的,JSONArray返回或传递回给调用类,如下所示。在这种情况下调用类的 MainClass 但也有它们使用相同的AsynTask类的其他活动课( GettingBeaconsList 和 GettingAirports )

onPostExecute of each AsynTask class or subclass, JSONArray is returned or passed back to the calling class, shown below. In this case calling class is MainClass but there are other activity classes which use same AsynTask Classes(GettingBeaconsList and GettingAirports)

protected void onPostExecute(String file_url)   
{           
    pDialog.dismiss();      
    delegate.JsonArrayLoaded(gotNearestbeacons);
}

现在我有一种方法(JsonArrayLoaded) MainClass ,以解决两个响应来自两个不同的后台任务或服务来了。我使用的条件,找出哪些服务/班或AsynTask执行。

Now I have one method (JsonArrayLoaded) in MainClass to tackle two response coming from two different background task or services. I am using condition to figure out which service/class or AsynTask is executed.

但我要求解决这样的场景,如果我们在未来的5个或更多的后台服务,他们也只是返回一个JSON数组所以我需要做独立的接口为每个服务的最佳方式是什么?

But I am asking for the best way to tackle such scenario as if we have 5 or more background services in future and they just also return a JSON Array so do I need to make separate interfaces for each services ?

应该是什么的面向对象的出路这种情况?

What should be object oriented way out to this case ?

推荐答案

最简单的解决方案,我能想到的是修改 DelegateTaskCompleted 接口,所以它看起来是这样的

The most simplistic solution I can think of is to modify your DelegateTaskCompleted interface so as it looks like this:

public interface DelegateTaskCompleted{
  public void JsonArrayLoaded(AsyncTask<String, String, String> task, 
                              JSONArray result);
}

那么你的 onPostExecute 将派出自己回来就好如下:

Then your onPostExecute will send itself back like below:

protected void onPostExecute(String file_url)   
{           
    pDialog.dismiss();      
    delegate.JsonArrayLoaded(this, gotNearestbeacons);
}

最后,在你的 MainClass ,您可以根据的AsyncTask 的类型条件检查:

Finally, in your MainClass, you can have a conditional check based on the type of AsyncTask:

 @Override
 public void JsonArrayLoaded(AsyncTask<String, String, String> task,
                             JSONArray result) 
    {
         if(task instanceof GettingBeaconsList) { 
                //  do stuff related to the beacon list
         }

         else if(task instanceof GettingAirports) { 
            // do airport list stuff
         }

    }

这样,你可以很容易地识别的AsyncTask 发送通过应答,而不需要跟踪它是,如果一个人需要更长的时间比其他等。

That way you can easily identify the AsyncTask that sends through the response without having to track which it is, if one takes longer than the other etc.

另外,有扩展的AsyncTask 但是对于识别增加了一个抽象的变量其他类。

Alternatively, have another class that extends AsyncTask but adds an abstract variable for identification.

public class TrackableAsyncTask extends AsyncTask<String, String, String>{
    public abstract int getId();

    public static final int ID_AIRPORT = 1;
    public static final int ID_BEACONS = 2;
    ... etc
}

然后让你的机场和信标 AsycTasks 扩展这个来代替。这将要求他们实施的getId 方法。

Then have your Airport and Beacon AsycTasks extend this instead. This will require them to implement the getId method.

public class GettingAirports extends AsyncTask<String, String, String> {
     public int getId(){
        return ID_AIRPORT;
     }
}

然后,而不是做一个条件如果(任务的instanceof GettingAirports)你可以做一个开关语句如下图所示

And then instead of doing a conditional if (task instanceof GettingAirports) you can do a switch statement like below:

switch(task.getId()){
   case TrackableAsyncTask.ID_AIRPORT:
       // run airport code
}

希望这有助于。

Hope this helps.