사용자가 응용 프로그램을 시작하면 타이머가 시작되는 응용 프로그램이 있습니다. 10 초 후 AlertDialog
은 15 초 동안 리밍을 표시하고 타이머를 표시하고 14 초 후에 사라집니다. 이것은 응용 프로그램의 첫 번째 작업에서 잘 작동합니다. 사용자가 처음으로 Activty
->TimedNotify
Activity
에서 통과하면 10 초 후 타이머가 중지됩니다. onUserInteraction()
에 TimedNotify
타이머가 다시 시작되고 정상적으로 작동합니다. 내가 잘못하고있는 곳을 도와주세요.경고 대화 상자가 나타나지 않습니다.
public void onClick(DialogInterface dialog2, int iwhich) {
Intent in = new Intent(TimedAlert.this,
FirstActivity.class);
in.setAction(Intent.ACTION_MAIN);
in.addCategory(Intent.CATEGORY_LAUNCHER);
onUserInteraction();
}
당신은 startActivity(in);
이없는 모든 매개 변수를 설정 한 후 :
public class FirstActivity extends TimedNotify{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.timercheck);
final Button btnstart2 = (Button) findViewById(R.id.btn);
btnstart2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,
TimedNotify.class);
startActivity(intent);
}
});
}
}
public class TimedAlert extends Activity
{
static CountDownTimer timer1, timer2;
int flag = 0;
protected static final String TAG = null;
public static AlertDialog alert, alertdialog;
private static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView mCounter1TextField = (TextView) findViewById (R.id.mCounter1TextField);
// first timer set for 10sec
timer1 = new CountDownTimer(10000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
Log.v(TAG, "timer1 ticking");
mCounter1TextField.setText("Seconds left: "
+ formatTime(millisUntilFinished));
}
public void onFinish() {
//after 10sec display alert box and show timer
Log.v(TAG, "timer1 finished");
timer1.cancel();
AlertDialog.Builder builder = new AlertDialog.Builder(
TimedAlert.this);
builder.setTitle("Session Time Out");
builder.setMessage("00:15");
builder.setPositiveButton("Resume", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog2,int iwhich)
{
Intent in = new Intent(TimedAlert.this,FirstActivity.class);
//in case there are many events ..the intent should be passed to the last activity on clicking resume
in.setAction(Intent.ACTION_MAIN);
in.addCategory(Intent.CATEGORY_LAUNCHER);
onUserInteraction();
}
});
builder.setNegativeButton ("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog2,int iwhich)
{
timer2.cancel();
timer1.start();
}
});
alert = builder.create();
alert.show();
timer2 = new CountDownTimer(15000, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
Log.v(TAG, "timer2 ticking");
alert.setMessage("Your Session will expire in 5 minutes . Timleft00:"+ (millisUntilFinished/1000));
mCounter1TextField.setText("Seconds left: "+ formatTime (millisUntilFinished));
}
//after 15 sec dismiss alert box
public void onFinish() {
Log.v(TAG, "timer2 finished");
timer2.cancel();
alert.dismiss();
}
}.start();
}
}.start();
}
@Override
public void onBackPressed() {
Intent in = new Intent(TimedAlert.this, FirstActivity.class);
startActivity(in);
}
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis/1000;
long minutes = seconds/60;
seconds = seconds % 60;
minutes = minutes % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
output = minutesD + " : " + secondsD;
return output;
}
public void onUserInteraction() {
super.onUserInteraction();
// Remove any previous callback
try {
Log.v(TAG, "user interacted");
timer1.start();
timer2.cancel();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v(TAG, "paused");
onUserInteraction();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.v(TAG, "resumed");
onUserInteraction();
}
private void handleIntent(Intent intent) {
timer1.start();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v(TAG, "stopped");
timer1.start();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v(TAG, "started");
timer1.start();
}
}
그 코드는 매우 지저분하므로 두려워하지만 검토 할 수는 없습니다. 그러나 문제가 더 복잡해 졌다고 생각합니다. 예를 들어 두 개의 중첩 된 타이머를 사용하는 대신 단일 25 초 타이머를 사용하고 'Intent'를 호출하는 대신 클래스 내에서 메소드를 호출하면됩니다. –
지저분한 코드에 대해 정말 유감스럽게 생각합니다. 이해할 수있게 만들 것입니다. 두 번째 타이머에서 onTick 메서드를 호출하여 경고 상자에 남은 시간을 표시하려고하기 때문에 두 개의 타이머가 있습니다. 친절하게 제안합니다. 더 좋은 방법이 있다면 .. 더 나은 접근 방식을 선사합니다. – user1420943