Android 如何在给定代码中每1分钟递归生成AlertDialog?

Android 如何在给定代码中每1分钟递归生成AlertDialog?,android,Android,下面是启动手电筒的代码,但每1分钟后,它应询问“如果用户按ok,是否继续”,然后在1分钟后,它应显示ALertDialog,直到用户在Alert Dialog中按cancel。 在这段代码中,1分钟后只显示1次alertdialog,但是如何递归地生成它,有人能帮忙吗 package com.example.android.flashlight1; import android.app.AlertDialog; import android.content.Context; im

下面是启动手电筒的代码,但每1分钟后,它应询问“如果用户按ok,是否继续”,然后在1分钟后,它应显示ALertDialog,直到用户在Alert Dialog中按cancel。 在这段代码中,1分钟后只显示1次alertdialog,但是如何递归地生成它,有人能帮忙吗

       package com.example.android.flashlight1;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;


public class MainActivity extends AppCompatActivity
{
    private  Runnable mStatusChecker;
    private int mInterval = 60000;
    private Handler mHandler;
    private AlertDialog.Builder alertdialog;
    private boolean isLightOn=false;
    private Camera camera;
    private Button btn;
    private Context context;
    private static final ScheduledExecutorService worker =
            Executors.newSingleThreadScheduledExecutor();


    @Override
    protected void onStop()
    {
        super.onStop();

        if(camera!= null)
            camera.release();
    }



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button);
        context=this;
        PackageManager pk= context.getPackageManager();

        if(!pk.hasSystemFeature(PackageManager.FEATURE_CAMERA))
        {
            Log.e("err", "Device has no camera");
            return;
        }
        camera=camera.open();
        final Camera.Parameters p= camera.getParameters();
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                if (isLightOn)
                {
                    Log.i("info", "FlashLight is turn off");
                    p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    camera.setParameters(p);
                    camera.stopPreview();
                    isLightOn = false;

                }
                else
                {
                    Log.i("info", "FlashLight is turn On!");
                    p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(p);
                    camera.startPreview();
                    isLightOn = true;
                    startRepeatingTask();
                  /*  new Handler().postDelayed(new Runnable() {
                        //Using handler with postDelayed called runnable run method
                        @Override
                        public void run() {
                            //Show your Alert box here
                            new AlertDialog.Builder(context) // you can use getApplicationContext() or your activityname.this as context
                                    .setTitle("Do You Want to continue")
                                    .setMessage("Are you sure you want to continue?")
                                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            //Do nothing, Torch continues
                                            //worker.schedule(run(),5, TimeUnit.SECONDS);
                                            dialog.dismiss();
                                            startRepeatingTask();


                                        }
                                    })
                                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            // torch off
                                            Log.i("info", "FlashLight is turn off");
                                            p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                                            camera.setParameters(p);
                                            camera.stopPreview();
                                            isLightOn = false;
                                            dialog.dismiss();
                                            stopRepeatingTask();
                                        }
                                    })
                                            // .setIcon(android.R.drawable.ic_launcher.png)
                                    .show();

                        }
                    }, 60000); //60000milliseconds = 60 sec = 1min.

                    // It will show alertbox after 1 min .*/

                }
            }
        });
        alertdialog = new AlertDialog.Builder(this);
        mHandler =new Handler();
        mStatusChecker = new Runnable()
        {
            @Override
            public void run()
            {
                try {
                    //
                    //Show your Alert box here

                    alertdialog
                            .setTitle("Do You Want to continue")
                            .setMessage("Are you sure you want to continue?")
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    //Do nothing, Torch continues


                                    dialog.dismiss();

                                }
                            })
                            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    // torch off
                                    Log.i("info", "FlashLight is turn off");
                                    p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                                    camera.setParameters(p);
                                    camera.stopPreview();
                                    isLightOn = false;
                                    dialog.dismiss();
                                    stopRepeatingTask();

                                }
                            })
                                    // .setIcon(android.R.drawable.ic_launcher.png)
                            .show();

                }
                finally
                {
                    // 100% guarantee that this always happens, even if
                    // your update method throws an exception
                    mHandler.postDelayed(mStatusChecker, mInterval);
                }
            }

        };



    }
    void startRepeatingTask()
    {
        mStatusChecker.run();
    }
    void stopRepeatingTask()
    {
        mHandler.removeCallbacks(mStatusChecker);
    }

}