Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. public class MainActivity extends Activity implements OnClickListener, Runnable {
  2. Context context;
  3.  
  4. EditText editTextNum, editText, editUserName, editPassword;
  5. Button btnsend;
  6. ProgressDialog pd;
  7.  
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  12. setContentView(R.layout.activity_main);
  13. editUserName = (EditText) findViewById(R.id.edit_userName);
  14. editPassword = (EditText) findViewById(R.id.edit_password);
  15. editTextNum = (EditText) findViewById(R.id.edit_number);
  16. editText = (EditText) findViewById(R.id.edit_message);
  17. btnsend = (Button) findViewById(R.id.btnsend);
  18. btnsend.setOnClickListener(this);
  19. }
  20.  
  21. /** Called when the user clicks the Send button */
  22. public void sendMessage() {
  23. if (!isOnline()) {
  24. Toast.makeText(MainActivity.this,"No Internet Access..Cannot Send SMS", Toast.LENGTH_LONG).show();
  25. } else {
  26. String userName = editUserName.getText().toString();
  27. String password = editPassword.getText().toString();
  28. String number = editTextNum.getText().toString();
  29. String message = editText.getText().toString();
  30. String msgreciever = number;
  31. String testMessage = message;
  32. try {
  33. SmsSender.sendMessage(msgreciever, testMessage, userName, password);
  34. } catch (Exception ex) {
  35. Toast.makeText(MainActivity.this, "SMS Sending Failed.",Toast.LENGTH_LONG).show();
  36. }
  37.  
  38. }
  39. }
  40.  
  41. @Override
  42. public boolean onCreateOptionsMenu(Menu menu) {
  43. getMenuInflater().inflate(R.menu.activity_main, menu);
  44. return true;
  45. }
  46.  
  47. public boolean isOnline() {
  48. ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  49. NetworkInfo netInfo = cm.getActiveNetworkInfo();
  50. if (netInfo != null && netInfo.isConnectedOrConnecting()) {
  51. return true;
  52. }
  53. return false;
  54. }
  55.  
  56. public void onClick(View v) {
  57. // TODO Auto-generated method stub
  58. if (v == btnsend) {
  59. if (!isOnline()) {
  60. Toast.makeText(MainActivity.this,"No Internet Access..Cannot Send SMS",Toast.LENGTH_LONG).show();
  61. } else {
  62. pd = ProgressDialog.show(MainActivity.this, "Free Sms","Sending SMS..Please Wait..!!", true);
  63. Thread t = new Thread(this);
  64. t.start();
  65. }
  66.  
  67. }
  68. }
  69.  
  70. public void run() {
  71. // TODO Auto-generated method stub
  72. sendMessage();
  73. mHandler.sendEmptyMessage(0);
  74. }
  75.  
  76. public Handler mHandler = new Handler() {
  77. @Override
  78. public void handleMessage(Message msg) {
  79. // TODO Auto-generated method stub
  80. super.handleMessage(msg);
  81. pd.dismiss();
  82. Toast.makeText(MainActivity.this, "Message Sent Successfully",Toast.LENGTH_LONG).show();
  83. editTextNum.setText("");
  84. editText.setText("");
  85. editTextNum.requestFocus();
  86. }
  87. };
  88. }
  89.  
  90. Handler mHandler = new Handler(Looper.getMainLooper());
  91.  
  92. Runnable myRunnable = new Runnable() {
  93. public void run() {work here...}
  94. }
  95.  
  96. ...elsewhere
  97.  
  98. mHandler.post(myRunnable);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement