AlertDialogActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class AlertDialogActivity extends Activity { private static final int ALERTTAG = 0, PROGRESSTAG = 1; protected static final String TAG = "AlertDialogActivity"; private Button mShutdownButton = null; private DialogFragment mDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mShutdownButton = (Button) findViewById(R.id.shutdownButton); mShutdownButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialogFragment(ALERTTAG); } }); } public void showDialogFragment(int dialogID) { switch (dialogID) { case ALERTTAG: mDialog = AlertDialogFragment.newInstance(); mDialog.show(getFragmentManager(), "Alert"); break; case PROGRESSTAG: mDialog = ProgressDialogFragment.newInstance(); mDialog.show(getFragmentManager(), "Shutdown"); break; } } protected void continueShutdown(boolean shouldContinue) { if (shouldContinue) { mShutdownButton.setEnabled(false); showDialogFragment(PROGRESSTAG); finishShutdown(); } else { mDialog.dismiss(); } } private void finishShutdown() { new Thread(new Runnable() { @Override public void run() { try { // Pretend to do something before // shutting down Thread.sleep(5000); } catch (InterruptedException e) { Log.i(TAG, e.toString()); } finally { finish(); } } }).start(); } public static class AlertDialogFragment extends DialogFragment { public static AlertDialogFragment newInstance() { return new AlertDialogFragment(); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setMessage("Do you really want to exit?") .setCancelable(false) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { ((AlertDialogActivity) getActivity()) .continueShutdown(false); } }) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick( final DialogInterface dialog, int id) { ((AlertDialogActivity) getActivity()) .continueShutdown(true); } }).create(); } } public static class ProgressDialogFragment extends DialogFragment { public static ProgressDialogFragment newInstance() { return new ProgressDialogFragment(); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final ProgressDialog dialog = new ProgressDialog(getActivity()); dialog.setMessage("Activity Shutting Down."); dialog.setIndeterminate(true); return dialog; } } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/shutdownButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:text="ShutDown" android:textSize="24sp"> </Button> </LinearLayout> |