7
Reply

How to create alerts in android

Manish Dwivedi

Manish Dwivedi

11y
3.5k
1
Reply

    Dear, If You want to create real time alert then GCM (Google Cloud Messaging ) is best for Alert and Notification on device.If you want to local alert like data pack off Alert then Alert dialog and Toast is the way by which you can alert the user

    Android Tutorial- http://www.hub4tech.com/android-tutorial

    http://stackoverflow.com/questions/12600360/how-to-make-an-alert-dialog-box-in-android

    http://stackoverflow.com/questions/26097513/android-simple-alert-dialog

    http://stackoverflow.com/questions/12600360/how-to-make-an-alert-dialog-box-in-android

    http://www.wikihow.com/Show-Alert-Dialog-in-Android

    We need to use the AlertDialog package to create the Alert in Android

    the following program demonstartes how we can create a alert dialog box

    1. import android.content.DialogInterface;
    2. import android.support.v7.app.AlertDialog;
    3. import android.support.v7.app.AppCompatActivity;
    4. import android.os.Bundle;
    5. public class MainActivity extends AppCompatActivity {
    6. @Override
    7. protected void onCreate(Bundle savedInstanceState)
    8. {
    9. super.onCreate(savedInstanceState);
    10. setContentView(R.layout.activity_main);
    11. }
    12. // Declare the onBackPressed method
    13. // when the back button is pressed
    14. // this method will call
    15. @Override
    16. public void onBackPressed()
    17. {
    18. // Create the object of
    19. // AlertDialog Builder class
    20. AlertDialog.Builder builder
    21. = new AlertDialog
    22. .Builder(MainActivity.this);
    23. // Set the message show for the Alert time
    24. builder.setMessage("Do you want to exit ?");
    25. // Set Alert Title
    26. builder.setTitle("Alert !");
    27. // Set Cancelable false
    28. // for when the user clicks on the outside
    29. // the Dialog Box then it will remain show
    30. builder.setCancelable(false);
    31. // Set the positive button with yes name
    32. // OnClickListener method is use of
    33. // DialogInterface interface.
    34. builder
    35. .setPositiveButton(
    36. "Yes",
    37. new DialogInterface
    38. .OnClickListener() {
    39. @Override
    40. public void onClick(DialogInterface dialog,
    41. int which)
    42. {
    43. // When the user click yes button
    44. // then app will close
    45. finish();
    46. }
    47. });
    48. // Set the Negative button with No name
    49. // OnClickListener method is use
    50. // of DialogInterface interface.
    51. builder
    52. .setNegativeButton(
    53. "No",
    54. new DialogInterface
    55. .OnClickListener() {
    56. @Override
    57. public void onClick(DialogInterface dialog,
    58. int which)
    59. {
    60. // If user click no
    61. // then dialog box is canceled.
    62. dialog.cancel();
    63. }
    64. });
    65. // Create the Alert dialog
    66. AlertDialog alertDialog = builder.create();
    67. // Show the Alert Dialog box
    68. alertDialog.show();
    69. }
    70. }

    the above program will generte the an alert when we try to exit the app