Android如何在Android

时间:2017-04-20 15:14:37

标签: android splash-screen executorservice

我的场景是我有5个线程并行调用发生在启动活动但我需要等待一个特定的线程(这是DashBoardCallable)exectuion,以便我可以加载仪表板数据,显示启动屏幕,直到仪表板数据加载和一旦仪表板数据被加载,同时并行更改活动我在后台加载一些与该用户相关的数据(这是线程工具类会做的)。

或者您可以理解这种方式我有一个5个线程的ExecutorService用于并行调用,而Splashscreen基于5个线程执行。一旦完成此线程执行,请更改活动,让其他线程在后台执行。

下面是启动活动代码:

    ExecutorService executor = Executors.newFixedThreadPool(5);

            SharedPreferences        sharedpreferences = getSharedPreferences(getResources().getString(R.string.mypreference_key), Context.MODE_PRIVATE);
            final SharedPreferences.Editor editor = sharedpreferences.edit();
            ThreadUtil datatype1 = new ThreadUtil(editor,3504,"URL","DATATYPE1","DATATYPE1");
            ThreadUtil datatype2 = new ThreadUtil(editor,3504,"URL","DATATYPE2","DATATYPE2");
            ThreadUtil datatype3 = new ThreadUtil(editor,3504,"URL","DATATYPE3","DATATYPE3");
            ThreadUtil datatype4 = new ThreadUtil(editor,3504,"Different URL","DATATYPE1","DATATYPE1");
                    DashBoardCallable dashBoardCallable = new DashBoardCallable(SplashScreenActivity.this,3504);
    FutureTask<String> dashboardFuture = new FutureTask<String>(dashBoardCallable);
            executor.execute(datatype1);
            executor.execute(datatype2);
            executor.execute(datatype3);
            executor.execute(datatype4);
executor.execute(dashboardFuture);
            String response =dashboardFuture.get();

这是dashboaord callable:

public class DashBoardCallable implements Callable<String> {
    private Context context;
    private int user_id;
    public DashBoardCallable(Context context,int user_id){
        this.context = context;
        this.user_id = user_id;
    }

    @Override
    public String call() throws Exception {
        HttpUtil httpUtil = new HttpUtil();

        httpUtil.setUrl("URL");
        httpUtil.setType("GET");

        return httpUtil.getStringResponse();
    }
}

这是threadUtil类:

public class ThreadUtil implements Runnable {
    private int user_id;
    private String url,type,stored_name;
    private SharedPreferences.Editor editor;
    public ThreadUtil( SharedPreferences.Editor editor, int user_id, String url, String type,String stored_name){
        this.editor = editor;
        this.user_id = user_id;
        this.url = url;
        this.type = type;
        this.stored_name = stored_name;
    }

    @Override
    public void run() {
        HttpUtil httpUtil = new HttpUtil();
        httpUtil.setUrl(url);
        httpUtil.setType("GET");
        String jsonresponse =httpUtil.getStringResponse();
        Gson gson = new Gson();
        switch (type){
            case "DATATYPE1":
                saveDATATYPE1(jsonresponse,gson,editor);
                break;
            case "DATATYPE2":
                saveDATATYPE2(jsonresponse,gson,editor);
                break;
            case "DATATYPE3":
                saveDATATYPE3(jsonresponse,gson,editor);
                break;
        }


    }

    private void saveDATATYPE1(String jsonresponse, Gson gson,SharedPreferences.Editor editor) {
        if(!jsonresponse.equalsIgnoreCase("null")){
            editor.putString(stored_name, jsonresponse);
            editor.apply();
            editor.commit();
        }
    }

    private void saveDATATYPE2(String jsonresponse, Gson gson, SharedPreferences.Editor editor) {
        try {
            Type listType = new TypeToken<List<AssessmentPOJO>>() {}.getType();
            ArrayList<AssessmentPOJO> dashboardCards = (ArrayList<AssessmentPOJO>) gson.fromJson(jsonresponse, listType);
            for(AssessmentPOJO assessmentPOJO:dashboardCards){
                System.out.println("XXBBXBXBXBXB -> "+assessmentPOJO.getName());
                if(assessmentPOJO != null){
                    editor.putString(stored_name+assessmentPOJO.getId(), gson.toJson(assessmentPOJO));
                    editor.apply();
                    editor.commit();
                }
            }
        }catch (JsonSyntaxException jse){
            jse.printStackTrace();
        }catch (Exception e){

        }
    }
    private void saveDATATYPE3(String jsonresponse, Gson gson,SharedPreferences.Editor editor) {
        if(!jsonresponse.equalsIgnoreCase("null")) {
            Type listType = new TypeToken<List<CoursePOJO>>() {}.getType();
            ArrayList<CoursePOJO> coursePOJOs = (ArrayList<CoursePOJO>)gson.fromJson(jsonresponse, listType);
            for(CoursePOJO coursePOJO:coursePOJOs){
                if(coursePOJO != null){
                    editor.putString(stored_name+coursePOJO.getId(), gson.toJson(coursePOJO));
                    editor.apply();
                    editor.commit();
                }
            }
        }
        }
}

这是HttpUtil类:

public class HttpUtil {
    private String url;
    private String type;
    private HashMap<String,String> param;
    private String postrequest;
    public HttpUtil(){}
    private int socketTimeOut=0, connectionTimeOut=0;

    public HttpUtil(String url, String type, HashMap<String, String> param,String postrequest) {
        this.url = url;
        this.type = type;
        this.param = param;
        this.postrequest = postrequest;
    }


    public String getStringResponse(){
        String jsonresponse="";
        try {
            System.out.println("url "+url);
            System.out.println("type "+type);

            HttpResponse httpResponse = getHttpResponse();
            if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
                HttpEntity httpEntity = httpResponse.getEntity();
                jsonresponse = EntityUtils.toString(httpEntity);
                if(jsonresponse.equalsIgnoreCase("[]")){
                    jsonresponse="";
                }
                System.out.println("HttpUtil Response is .... " + jsonresponse);
            } else {
                return "null";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonresponse;
    }
    public void getVoidResponse(){

    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public HashMap<String, String> getParam() {
        return param;
    }

    public void setParam(HashMap<String, String> param) {
        this.param = param;
    }

    private HttpResponse getHttpResponse(){
        HttpResponse httpResponse = null;
        HttpClient httpclient = new DefaultHttpClient();
        try{
        switch(type){
            case "GET":
                if(socketTimeOut != 0 && connectionTimeOut != 0){
                    HttpParams httpParameters = new BasicHttpParams();
                    HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOut);
                    HttpConnectionParams.setSoTimeout(httpParameters, socketTimeOut);
                    httpclient = new DefaultHttpClient(httpParameters);
                }
                httpResponse = httpclient.execute(new HttpGet(url));

                break;
            case "POST":
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                if(param != null) {
                    for (String key : param.keySet()) {
                        nameValuePairs.add(new BasicNameValuePair(key, param.get(key)));
                    }
                }
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpResponse = httpclient.execute(httpPost);
                break;
            case "PUT":
                HttpPut httpPut = new HttpPut(url);
                if(postrequest != null){
                    StringEntity se = new StringEntity(postrequest);
                    se.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
                    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
                    httpPut.setEntity(se);
                    httpPut.setHeader("Accept", "application/json");
                    httpPut.setHeader("Content-type", "application/json");
                }
                httpResponse = httpclient.execute(httpPut);
                break;
            default:
                httpResponse = httpclient.execute(new HttpGet(url));
                break;
        } }catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }catch (JsonSyntaxException jse) {
            jse.printStackTrace();
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return httpResponse;
    }

    public String getPostrequest() {
        return postrequest;
    }

    public void setPostrequest(String postrequest) {
        this.postrequest = postrequest;
    }

    public int getSocketTimeOut() {
        return socketTimeOut;
    }

    public void setSocketTimeOut(int socketTimeOut) {
        this.socketTimeOut = socketTimeOut;
    }

    public int getConnectionTimeOut() {
        return connectionTimeOut;
    }

    public void setConnectionTimeOut(int connectionTimeOut) {
        this.connectionTimeOut = connectionTimeOut;
    }
}

2 个答案:

答案 0 :(得分:0)

基于你的线程的使用,我可以告诉你应该更好地使用AsyncTask。

每个AsyncTask都必须有一个Callback,并且Activity实现它们,因此,在任务完成后,Activity可以控制操作并做出决定。

对于任务:

public class SplashScreenTask extends AsyncTask<Void, Void, Void> {

private Context context;

private SplashScreenTaskCallback listener = null;

public SplashScreenTask (Context context) {
    this.context = context;
}

@Override
protected Void doInBackground (Void... params) {

    // Do your tasks


    return null;
}

@Override
public void onPreExecute () {
}

@Override
public void onPostExecute (Void v) {
    if (listener != null) {
        listener.OnSplashScreenTaskCompleted ();
    }
}

public void setListener (SplashScreenTaskCallback listener) {
    this.listener = listener;
}


public interface SplashScreenTaskCallback {
    void OnSplashScreenTaskCompleted ();
}
}

您的活动:

public class SplashScreenActivity extends Activity implements SplashScreenTask.SplashScreenTaskCallback {

protected Runnable postDelayedAction;
private final Handler handler = new Handler();

@Override
public void onCreate (Bundle savedInstanceState) {

    Log.d(TAG, "onCreate");

    super.onCreate(savedInstanceState);

    handler.postDelayed(startBackgroundTasks, splashScreenDelay);

}

private Runnable startBackgroundTasks = new Runnable() {

    @Override
    public void run () {

        //  Do initial background tasks like sounds load
        SplashScreenTask task = new SplashScreenTask (SplashScreenActivity.this);
        task.setListener (SplashScreenActivity.this);
        task.execute ();

    }
};

@Override
public void OnSplashScreenTaskCompleted () {
    // Here you take decisions
}
}

答案 1 :(得分:0)

我会使用Rx zip操作符来确定所有后台任务何时完成。确定多个并行作业的完成是Android框架的一个巨大问题。

最佳做法是显示没有初始延迟的闪屏,其中您使用主题设置背景可绘制。为了做到这一点并转换到您可以实际触发所有这些逻辑的Activity(而不是你的启动画面),只需在Activity中复制启动画面的视觉效果,并在作业完成后转换为显示这些视觉效果完成。