我添加了一个progressbar
作为不确定状态,并放入一个扩展asynctask
的类中。
在doInBackground
方法中,我用m_one
添加了一个方法(例如volley-library
)。
在m_one()
中,我调用了方法m_two();
。
在m_two()
中,我打电话给m_three();
。
现在的问题是,progressbar
仅在执行m_one()
之前有效,然后progressbar
的可见性变为不可见,其余方法在后台线程中执行。
在所有方法都使用截击请求接收数据之前,我如何继续旋转progressbar
?
答案 0 :(得分:0)
尝试此代码。
private static final String TAG = "LoginActivity.this";
Button button_login;
Context context;
ProgressBar progressBar;
//some variables for accepting the volley response data
DBUtils dbUtils;
Calendar calendar_2;
public SimpleDateFormat dateFormat;
public SimpleDateFormat timeformat;
Date date;
TreeSet<Integer> idSet;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
button_login = findViewById(R.id.btn_login);
context = getApplicationContext();
progressBar = findViewById(R.id.progress_id_1);
calendar_2 = Calendar.getInstance();
dbUtils = new DBUtils(this);
//clear the db tables on start of app
dbUtils.ClearTables();
date = Calendar.getInstance().getTime();
Log.e(TAG, "\n current time: " + date);
dateFormat = new SimpleDateFormat("dd-MM-yyyy");
idSet = new TreeSet<>();
//Login button click event ********************
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkNetwork();
}
});
}
private void checkNetwork() {
progressBar.setVisibility(View.VISIBLE);
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
try {
if (netInfo != null) {
NetworkInfo.State state = conMgr.getActiveNetworkInfo().getState();
if (state == NetworkInfo.State.CONNECTED) {
Log.e(TAG, "nw info: " + netInfo.getExtraInfo());
Log.e(TAG, "Network Connected ");
method1();
Thread.sleep(3000);
} else {
Log.e(TAG, "Network Disconnected");
}
} else if (netInfo == null) {
Log.e(TAG, "Network Not Found !");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// ********************************************
// ********************************************
//to get the response of requested url
public void method1() {
try {
//url without api for login session
URL loginURL = new URL(your url 1);
HttpURLConnection urlconnect = (HttpURLConnection) loginURL.openConnection();
;
//http connection for webpage permission
//type of request get / post...
urlconnect.setRequestMethod("POST");
//allow the post request for user
urlconnect.setDoOutput(true);
/*
Singleton_Volley is the class for volley initialize
you can skip Singleton_Volley class..its correct
*/
// Get a RequestQueue in required page
RequestQueue queue = Singleton_Volley.getInstance(this.getApplicationContext()).
getRequestQueue();
// Request a string response from the provided URL.(my case, url)
StringRequest stringRequest = new StringRequest(Request.Method.POST, "" + loginURL.toString(),
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//accept the data from request and set in our views like list,text, etc...
try {
/* your response data stored in variables first and then stored in sqlite you have to consider this strictly, make sure you also have array in response to wait for a while */
method2();
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//popup an error message...(Toast)
}
})
} ;
// Add the request to the RequestQueue.
Singleton_Volley.getInstance(this).addToRequestQueue(queue.add(stringRequest));
} catch(
IOException e)
{
e.printStackTrace();
} catch(
JSONException e)
{
e.printStackTrace();
}
}
public void method2() {
try {
url_2 = new URL(your url 2);
HttpURLConnection urlconnect = (HttpURLConnection)
url_2.openConnection();
;
//http connection for webpage permission
//type of request get / post...
urlconnect.setRequestMethod("POST");
//allow the post request for user
urlconnect.setDoOutput(true);
RequestQueue contactQueue = Singleton_Volley.getInstance(this).
getRequestQueue();
// Request a string response from the provided URL.(in my case, url_2)
StringRequest stringRequest = new StringRequest(Request.Method.GET, "" + url_2.toString(),
new Response.Listener<String>() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(String response) {
//accept the data from request and set in our views like list,text, etc...
try {
/*
your response data stored in variables first and then stored in sqlite
you have to consider this strictly
make sure you also have array in response to wait for a while
*/
method3(); //method3() called here
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//popup an error messege...(Toast)
Toast.makeText(LoginActivity.this, "" + error, Toast.LENGTH_SHORT).show();
}
})
} ;
// Add the request to the RequestQueue.
Singleton_Volley.getInstance(this).addToRequestQueue(contactQueue.add(stringRequest));
} catch(Exception e){
e.printStackTrace();
}
}
// ********************************************
// ********************************************
private void method3(){
try{
URL url_3=new URL(your url 3);
HttpURLConnection urlconnect=(HttpURLConnection)url_3.openConnection();
;
//http connection for webpage permission
//type of request get / post...
urlconnect.setRequestMethod("POST");
//allow the post request for user
urlconnect.setDoOutput(true);
RequestQueue stageQueue=Singleton_Volley.getInstance(this).
getRequestQueue();
// Request a string response from the provided URL.(in my case, url_3)
StringRequest stringRequest=new StringRequest(Request.Method.GET,""+url_3.toString(),
new Response.Listener<String>(){
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(String response){
//accept the data from request and set in our views like list,text, etc...
try{
progressBar.setVisibility(View.GONE);
/*
make sure you also have array in response to wait for a while
*/
}catch(Exception ex){
ex.printStackTrace();
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
//popup an error messege...(Toast)
}
})
};
// Add the request to the RequestQueue.
Singleton_Volley.getInstance(this).addToRequestQueue(stageQueue.add(stringRequest));
}catch(Exception ec){
ec.printStackTrace();
}
startActivity(new Intent(LoginActivity.this,Page2.class));
}
}
}}
请管理结束语},它应该可以工作。