进度对话框不会出现

时间:2012-09-01 04:59:01

标签: android sms progressdialog

让我首先澄清一下,我仍然是一个非常业余的Android开发人员,大约一周前开始学习http://developer.android.com/training/basics/firstapp/index.html上的教程

所以,如果我的代码看起来很难看,原谅我......

现在实际问题 - “进度对话框没有出现” - 但其余代码工作正常。它正在发送短信,我正在接收它们,只是遇到了PROGRESS DIALOG的问题。

public class MainActivity extends Activity {
Context context;

public final static String EXTRA_NUMBER = "com.example.myfirstapp.NUMBER";
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    if (!isOnline()) {
        Toast.makeText(MainActivity.this,"No Internet Access..Cannot Send SMS", Toast.LENGTH_LONG).show();
    } else {

        ProgressDialog prgDialog = ProgressDialog.show(MainActivity.this,"Free Sms","Sending SMS..Please Wait..!!",true);           
        EditText editTextNum = (EditText) findViewById(R.id.edit_number);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String number = editTextNum.getText().toString();
        String message = editText.getText().toString();
        try {
            prgDialog.dismiss();
            String msgreciever = number;
            String testMessage = message;
            SmsSender.sendMessage(msgreciever, testMessage);                
            Toast.makeText(MainActivity.this, "Message Sent Successfully",Toast.LENGTH_LONG).show();
            editTextNum.setText("");
            editText.setText("");
        } catch (Exception ex) {
            prgDialog.dismiss();
            Toast.makeText(MainActivity.this, "Message Sending Failed",Toast.LENGTH_LONG).show();
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

}

2 个答案:

答案 0 :(得分:1)

您正在致电

prgDialog.dismiss

之后立即

prgDialog.show.

这就是它没有出现的原因。

编辑: 添加以下代码而不是消除,这应该在3秒后删除对话框

new CountDownTimer(3000, 1000) 
 {
      @Override
       public void onTick(long l) {
       }

       @Override
       public void onFinish(){
             prgDialog.dismiss();
       };
 }.start();

答案 1 :(得分:0)

Hello Tapan实际上你必须使用“Thread”或“Runnable”进行这种操作......所以在代码的“else”部分放置这个使用Thread的修改后的代码..所以它肯定会为你工作....

EditText editTextNum = (EditText) findViewById(R.id.edit_number);
EditText editText = (EditText) findViewById(R.id.edit_message);
final String number = editTextNum.getText().toString();
final String message = editText.getText().toString();
final ProgressDialog prgDialog = ProgressDialog.show(this,"Free Sms","Sending SMS..Please Wait..!!",true);
Thread t=new Thread(new Runnable() {
    public void run() {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(1000);
            SmsSender.sendMessage(number, message);
            prgDialog.dismiss();
            Toast.makeText(MainActivity.this, "Message Sent Successfully",Toast.LENGTH_LONG).show();

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
});
t.start();
editTextNum.setText("");
editText.setText("");