我使用地震与下面的Android代码。
当用户摇动消息框时多次出现。我将如何修改代码以使其在摇动之后停止检测'并等待消息框被解除,然后再次检测到震动?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.squareup.seismic.ShakeDetector;
import com.squareup.seismic.ShakeDetector.Listener;
import android.hardware.SensorManager;
public class MainActivity extends AppCompatActivity implements Listener {
public void clickFunction(View view) {
new AlertDialog.Builder(this)
.setTitle("Not Signed In")
.setMessage("You need to be signed in to use this feature.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Hello", "created!");
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
ShakeDetector sd = new ShakeDetector(this);
sd.start(sensorManager);
}
public void hearShake() {
Log.i("wooo", "Shaken!");
new AlertDialog.Builder(this)
.setTitle("Not Signed In")
.setMessage("SHAKEN!.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
答案 0 :(得分:1)
而不是new AlertDialog.Builder...show();
我建议分配一个AlertDialog
变量(例如mDialog
),而只需添加hearShake()
if(mDialog.isShowing()) return;
更新:让sd
成为全局变量
ShakeDetector sd = new ShakeDetector(this);
//...
protected void onCreate(...){
//...
sd = new ShakeDetector(this);
sd.start(sensorManager);
}
public void hearShake(){
//...
sd.stop();
//...
}