如下代码,在调用newPicture时启动Intent,之后显示Dialog。这是什么意思,我该如何更改顺序?
public void newPicture(View v) {
SharedPreferences settings = getPreferences(MODE_PRIVATE);
boolean geoProtipAlreadyShown = settings.getBoolean("geoProtipAlreadyShown", false);
if (!geoProtipAlreadyShown) {
showGeoProtip();
// and set the option in SharedPreferences
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("geoProtipAlreadyShown", true);
editor.commit();
}
// start the image capture activity
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PATH, "tmpfile.jpg")));
startActivityForResult(intent, IMAGE_CAPTURE);
}
private void showGeoProtip() {
String geoProtip = this.getResources().getString(R.string.protip);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(geoProtip).setCancelable(true).setPositiveButton("OK", null);
AlertDialog alert = builder.create();
alert.show();
}
最佳答案
将启动图像捕获 Activity 移动到新方法,并将其放入对话框的 OnClickListener:
builder.setMessage(geoProtip).setCancelable(true).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
captureImage();
}
});
private void captureImage(){
// start the image capture activity
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(PATH, "tmpfile.jpg")));
startActivityForResult(intent, IMAGE_CAPTURE);
}
并修改if-else:
if (!geoProtipAlreadyShown) {
showGeoProtip();
....
}else{
captureImage();
}
关于android - 为什么 Intent 在 AlertDialog.Builder 之前启动,即使它的编码方式相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6150786/