我正在尝试制作一个用于个人物品的小型Android应用程序,现在我的代码有两个问题:


该代码应执行以下操作,直到



  isAlarm == false;


                do {
                (new BTask(context)).execute();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (isAlarm);


但这仅执行一次。


我希望它仅在输入正确密码的情况下关闭对话框,但它每次都关闭并且不执行命令:



  isAlarm = false;


我知道,代码不是很漂亮,但我仍在学习。 =)

package top.sysop.myapplication;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.ToneGenerator;
import android.os.AsyncTask;
import android.os.Vibrator;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "";
    public static Button btn1;
    public static Button btn2;
    public static Button btn3;
    private MediaPlayer mMediaPlayer;
    boolean isAlarm;
    private String m_Text = "";
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context=this;
        setContentView(R.layout.activity_main);
        dialogevent3();
    }
    public void dialogevent3() {
        int i=1;
        btn3 = (Button) findViewById(R.id.dial3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Diebstahl!");
// Set up the input
                final EditText input = new EditText(MainActivity.this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                builder.setView(input);
// Set up the buttons
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        m_Text = input.getText().toString();
                        if (m_Text.equals("pass"))
                        {
                            //end
                            isAlarm = false;
                        }
                        else
                        {
                            //repeat
                            dialog.dismiss();
                            dialogevent3();
                        }
                    }
                });
                do {
                    (new BTask(context)).execute();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } while (isAlarm);
            //    alert.show();
                builder.show();
            }
        });
    }
}

最佳答案

您没有初始化is Alarm的值,因此它包含默认值false。
有一些变化,例如
初始化boolean isAlarm = true
while (isAlarm)更改为while (!isAlarm)
希望这可以帮助

09-26 16:52