Android AsyncTask竞争条件

时间:2016-06-30 19:52:43

标签: android android-asynctask

我写了一些看起来很奇怪的代码。希望其他人看看它,看看他们是否能发现任何问题。我认为它可能会导致一些竞争条件,但我没有创建我的网络api所以我还没有广泛测试它。

在网络课程中进行回调是让我感到不安的原因。

网络课程 - 此课程将处理我的Android应用程序的所有后期操作

import android.graphics.Bitmap;
import android.os.AsyncTask;

public class NetworkSend extends AsyncTask<Void, Void, String> {
    private static NetworkSend singleton = new NetworkSend();
    final String BASE_URL = "http://azure/api";
    final String LOGOUT_URL = "/logout";
    final String PICTURE_NOTE_URL = "/picture_note";
    final String POST_LOCATION_URL = "/post_location";
    final String LOGIN_URL = "/login";

    String currentCall = null;
    String authKey = null;
    double lat = -1.0;
    double log = -1.0;
    String note = null;
    Bitmap picture = null;
    int warning = -1;
    boolean variablesSet = false;
    String status;
    String user;
    String password;
    NetworkCallBack callBack;
    String returnValue;

    private NetworkSend(){
        this.callBack = new NetworkCallBack() {
            @Override
            public void done(String status) {
                returnValue = status;
            }
        };
    }

    public static NetworkSend getInstance( ) {
        return singleton;
    }

//    public NetworkSend(NetworkCallBack callback){
//        this.callback = callback;
//    }

    public boolean setVariables(String key, double latitude, double logitude, String note, Bitmap picture){
        this.authKey = key;
        this.lat = latitude;
        this.log = logitude;
        this.note = note;
        this.picture = picture;
        this.currentCall = PICTURE_NOTE_URL;
        this.variablesSet = true;
        return true;
    }

    public boolean setVariables(String username, String password){
        this.currentCall = LOGIN_URL;
        this.user = username;
        this.password = password;
        return true;
    }

    public boolean setVariables(String key){
        this.authKey = key;
        this.currentCall = LOGOUT_URL;
        this.variablesSet = true;
        return true;
    }

    public boolean setVariables(String key, double latitude, double logitude, int warning){
        this.authKey = key;
        this.lat = latitude;
        this.log = logitude;
        this.warning = warning;
        this.currentCall = POST_LOCATION_URL;
        this.variablesSet = true;
        return true;
    }

    public boolean resetVariables(){
        this.currentCall = null;
        this.authKey = null;
        this.lat = -1.0;
        this.log = -1.0;
        this.note = null;
        this.picture = null;
        this.warning = -1;
        this.variablesSet = false;
        this.user = null;
        this.password = null;
        return true;
    }

    @Override
    protected String doInBackground(Void... params) {
        //resetVariables();
        status = this.authKey;
        return status;
    }

    @Override
    protected void onPostExecute(String status) {
        super.onPostExecute(status);
        callBack.done(status);
    }
}

网络回调 - 完成后处理警报流程

public interface NetworkCallBack {
    void done(String status);
}

测试类 - 目前只有1个测试

public class NetworkSendTests {
    NetworkSend network;

    @Test
    public void setVariables1 () {
        network = NetworkSend.getInstance();
        network.setVariables("apple");
        assertEquals(network.doInBackground(), "apple");
    }
}

我知道我可以做任何我需要进行网络通话的事情。我只是想减少代码重复。我相信我误用/完全无视了回调的指针。

network = new NetworkSend(new NetworkCallBack() {
    @Override
    public void done(String status) {
        Toast.makeText(Warning.this, "Network Status: " + status, Toast.LENGTH_LONG).show();
    }
}); 

0 个答案:

没有答案