stack
stack

Reputation: 21

android how can i remove alertdialog after 5 minutes?

I'd like to make alertdialog buddler which can disappear and automatically send sms when user doesn't click any button after 5 min. I can display alterdialog buddler but i don't know how to send sms..

This is my code. Please help me to correct it.

AlarmService_Servierce.java

public class AlarmService_Servierce extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
         final Calendar c = Calendar.getInstance();
        int mHour = c.get(Calendar.HOUR_OF_DAY);
        int mMinute = c.get(Calendar.MINUTE);
        int mSecond =c.get(Calendar.SECOND);        


         AlertDialog.Builder builder = new AlertDialog.Builder(context);


        builder.setTitle("NOTICE");
        builder.setIcon(R.drawable.notification_icon);
        builder.setMessage("notice message");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
                //Do something
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                 dialog.dismiss();
            }
     });
        AlertDialog alert = builder.create();
        alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alert.show();

    }

}

MainActivity.java

    Intent intent = new Intent(MainActivity.this, AlarmService_Service.class);
    PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this,
            0, intent, 0);


    long firstTime = SystemClock.elapsedRealtime();
    firstTime += 300*1000;      
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                    firstTime, 10*1000, sender);
      final Calendar c = Calendar.getInstance();
        int mHour = c.get(Calendar.HOUR_OF_DAY);
        int mMinute = c.get(Calendar.MINUTE);
        int mSecond =c.get(Calendar.SECOND);
    }
private void SendSMS(String phonenumber, String message) {
        SmsManager smsManager = SmsManager.getDefault();
        String sendTo = phonenumber;
        String myMessage = message;
        smsManager.sendTextMessage(sendTo, null, myMessage, null, null);

        finish();
    }

Upvotes: 1

Views: 311

Answers (2)

Sasi Kumar
Sasi Kumar

Reputation: 13348

final AlertDialog.Builder dialog = new    AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?");
 dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
  @Override
public void onClick(DialogInterface dialog, int whichButton) {
    exitLauncher();
}
});     
final AlertDialog alert = dialog.create();
alert.show();

// Hide after some seconds
final Handler handler  = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
    if (alert.isShowing()) {
        alert.dismiss();
    }
}
 };

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
    handler.removeCallbacks(runnable);
}
  });

 handler.postDelayed(runnable, 10000);//replace your time

Upvotes: 2

AnswerDroid
AnswerDroid

Reputation: 1893

Use the dismiss function wherever you wish to dismiss the dialog or when timer finishes.

alert.dismiss();

Upvotes: 0

Related Questions