我有两个Activity
。 MainActivity正在调用扩展Activity
的另一个AsyncTask
。在AsyncTask
我显示ProgressDailog
onPreExecute()
,但未显示ProgressDailog
。原因可能是AyncTask
执行时间较短但响应但响应需要时间,因此应显示ProgressDialog
。
提供以下代码。
Aysnc class
public class NewConnections extends AsyncTask<Void,Void,String>{
Context context;
String[] name,value;
String Geturl,para;
ProgressDialog progressDialog;
NewConnections(Context context,String[] name,String[] value,String Url){
this.context = context;
this.name = name;
this.value = value;
Geturl = Url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Please Wait");
progressDialog.show();
}
@Override
protected String doInBackground(Void... voids) {
try
{
para = DCBUtil.jsonCovertion(name,value);
URL url = new URL(Geturl);
String responseStr = XXX.callJsonHttp(url,para);
System.out.println("Response of Home Open FD :: "+responseStr);
return responseStr;
}catch(Exception e)
{
return "Error";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
}
}
MainClass
public class FirstPage extends AppCompatActivity {
Button existUser,newUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_page);
existUser = (Button)findViewById(R.id.existUser);
newUser = (Button)findViewById(R.id.newUser);
newUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] value = {"asdasd"};
String[] name = {"dprm"};
try {
NewConnections v= new NewConnections(FirstPage.this,name,value,"url is provided");
v.execute().get();
} catch (Exception e) {
e.printStackTrace();
}
}
});
答案 0 :(得分:0)
试试这个:
public class NewConnections extends AsyncTask<Void,Void,String>{
Context context;
String[] name,value;
String Geturl,para;
public static ProgressDialog progressDialog;
NewConnections(Context context,String[] name,String[] value,String Url){
this.context = context;
this.name = name;
this.value = value;
Geturl = Url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setMessage("Please Wait");
progressDialog.show();
}
@Override
protected String doInBackground(Void... voids) {
try
{
para = DCBUtil.jsonCovertion(name,value);
URL url = new URL(Geturl);
String responseStr = XXX.callJsonHttp(url,para);
System.out.println("Response of Home Open FD :: "+responseStr);
return responseStr;
}catch(Exception e)
{
return "Error";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(progressDialog!=null)
progressDialog.dismiss();
}
}
当您退出活动时,请在该活动的onDestry()方法中写下以下代码
@Override
protected void onDestroy() {
if(NewConnections.progressDialog!=null)
NewConnections.progressDialog.dismiss();
super.onDestroy();
}
答案 1 :(得分:0)
试试这个::我希望这可以帮到你。
Dialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressBar();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try{
hideProgressBar();
}catch (Exception e){
e.printStackTrace();
}
}
public void showProgressBar() {
dialog = new Dialog(mContext, android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
dialog.setCancelable(false);
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewChild = inflater.inflate(R.layout.loader, null);
dialog.setContentView(viewChild);
Runtime.getRuntime().gc();
System.gc();
try {
dialog.show();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* Hide progress dialog
*/
public void hideProgressBar() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
loader.xml::
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:id="@+id/loading_spinner"
android:layout_width="48dp"
android:layout_height="48dp"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/colorAccent"
android:layout_gravity="center" />
</RelativeLayout>