안드로이드 개발 질문/답변
(글 수 45,052)
package android.SosTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
public class WrongPW extends Activity {
public static final String KEY_MY_PREFERENCE = "my_preference";
public static final String KEY_TERM_PREFERENCE = "term_preference";
final static int RESULT_NOT = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
setContentView(R.layout.write_pw);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable(){
@Override
public void run() {
Intent iSms = new Intent(WrongPW.this, SendSms.class);
startActivity(iSms);
finish();
}
}, 60*1000);
final LinearLayout linear = (LinearLayout)View.inflate(this, R.layout.write_pw, null);
new AlertDialog.Builder(this)
.setTitle("비밀번호를 입력하세요.")
.setView(linear)
.setPositiveButton("확인",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO Auto-generated method stub
SharedPreferences pref = getSharedPreferences("PrefTest", 0);
int pw = pref.getInt("StPass", 0);
EditText textPW = (EditText)linear.findViewById(R.id.pw);
String pwSting = textPW.getText().toString();
int edpw = Integer.parseInt(textPW.getText().toString());
if(pw==edpw) {
Intent result = new Intent(WrongPW.this, Timer.class);
result.putExtra("wrongresult", true);
setResult(2,result);
finish();
}
else if(pw!=edpw){
if(pw==edpw) {
Intent result = new Intent(WrongPW.this, Timer.class);
result.putExtra("wrongresult", true);
setResult(2,result);
finish();
}
else if(pw!=edpw){
Intent result = new Intent(WrongPW.this, Timer.class);
result.putExtra("wrongresult", false);
setResult(2,result);
finish();
}
}
}
}).show();
}
}
60초동안 아무 일도 일어나지 않으면 이 엑티비티에서 다른 엑티비티로 넘어가고 싶어서 Handler를 사용했습니다.
그런데 문제는
액티비티에속해있는 다이어로그에서 비밀번호를 잘못 입력하거나, 알람 종료가 되지 않았을시 여러번 이 액티비티를 띄워야하는데, 처음 액티비티가 실행됐을때부터 60초가 지나면 계속 실행중인데도 불구하고 다른 액티비티로 넘어가 버립니다.
그리구 이 프로그램이 종료되었을때도 다른 액티비티를 계속 띄우는데요.
아마 Handler종료를 하지 않아서 그런거 같은데 Handler를 종료할 수 있는 방법 있나요?
아니면 다른 문제인가요?




프로그램을 작성해 놓으신걸보면.. 해당 엑티비티가 생성이되고 무조건 60초뒤에 다른 엑티비티로 전환되도록 구현이 되어 있네요.
mHandler.postDelay( ...) 이 부분이 문제가되는 것 같네요.
그리고 원하시는 기능이 뭔지 제가 이해가 좀 안가는데.. 60초안에 암호 입력을 못하면 이 엑티비티를 다시 띄워주실려고 하시는건가요? 그렇다면 타이머를 사용하시거나, 엑티비티가 생성이 될 때 60초를 카운트하는 쓰레드를 생성하고 60초 뒤에 핸들러에게 메시지를 발생하도록 작성하시면 될 듯하네요.
핸들러에 대해서 좀 더 이해를 하시면 쉽게하실 수 있을 것 같습니다.