我正在尝试为个人创建一个小型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();
}
});
}
}
答案 0 :(得分:3)
您未初始化 isAlarm 的值,因此其中包含默认值 false 。 有一些变化,如
初始化boolean isAlarm = true
将while (isAlarm)
更改为while (!isAlarm)
希望这有帮助
答案 1 :(得分:1)
您可以使用TextWatcher和runnable实现此连续检查。
custom_layout_for_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Password..."/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ems="10"
android:inputType="textPassword"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Password here"
/>
</LinearLayout>
调用此方法从您的Activity
打开自定义对话框public void customDialog()
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_layout_for_dialog);
dialog.setCancelable(true);
EditText edt_password= (EditText) dialog.findViewById(R.id.editText);
edt_password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//write all your operations here
String m_Text=s.toString();
if (m_Text.equals("pass"))
{
//end
isAlarm = false;
}
else
{
//repeat
dialog.dismiss();
dialogevent3();
}
}
});
//This handler checks for isAlarm for every 300ms
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (isAlarm){
(new BTask(context)).execute();
}
}
},300);
dialog.show();
}
答案 2 :(得分:0)
谢谢你们 我用以下代码解决了它:
MainActivity.java
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;
public static 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);
dialogevent1();
dialogevent2();
dialogevent3();
}
public void dialogevent1() {
btn1 = (Button) findViewById(R.id.dial1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder altdial = new AlertDialog.Builder(MainActivity.this);
altdial.setMessage("Bitte geben Sie mich bei der Kasse ab.").setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("xxx");
alert.show();
}
});
}
public void dialogevent2() {
btn2 = (Button) findViewById(R.id.dial2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder altdial = new AlertDialog.Builder(MainActivity.this);
altdial.setMessage("Bitte geben Sie mich sofort bei der Kasse ab!").setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("xxx");
alert.show();
}
});
}
public void dialogevent3() {
isAlarm = true;
btn3 = (Button) findViewById(R.id.dial3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buildDialog();
/* AlertDialog.Builder altdial = new AlertDialog.Builder(MainActivity.this);
altdial.setMessage("Sie begehen Diebstahl!").setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
isAlarm = false;
dialogInterface.cancel();
}
});
AlertDialog alert = altdial.create();
alert.setTitle("xxx");*/
// alert.show();
}
});
}
private void buildDialog() {
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.setNeutralButton("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();
buildDialog();
input.setText("");
}
}
});
(new BTask(context)).execute();
builder.show();
}
}
和
BTask.java
package top.sysop.myapplication;
import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.AsyncTask;
import android.os.Vibrator;
/**
* Created by Szalewicz on 22.11.2017.
*/
public class BTask extends AsyncTask {
Context context;
public BTask() {
}
public BTask(Context context) {
this.context = context;
}
@Override
protected Object doInBackground(Object[] objects) {
final Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
final ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
do {
v.vibrate(500);
toneGen1.startTone(ToneGenerator.TONE_CDMA_MED_SSL, 500);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (MainActivity.isAlarm);
//for (int faktor = 1; faktor <= 10; faktor ++) {
v.vibrate(500);
toneGen1.startTone(ToneGenerator.TONE_CDMA_MED_SSL, 500);
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//}
return false;
}
}
现在它正在做我想要的事情=)