• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Alarm problem

Hello,

in my app, when user clicks a button, and alarm should start in 5 seconds, but it starts as soon as button is clicked.

Code:
b2 = (Button) findViewById(R.id.Button02);
    	b2.setOnClickListener(new View.OnClickListener() {
    	public void onClick(View v) {
    		
    		if (editText.getText().toString().equals(""))
    		{ 
    			AlertDialog ad2 = new AlertDialog.Builder(HelloAndroid2.this).create();  
        		ad2.setCancelable(false);
        		ad2.setMessage((CharSequence) "Please give a medicin name");
        		ad2.setButton("OK", new DialogInterface.OnClickListener() {  
        		    public void onClick(DialogInterface dialog, int which) {  
        		        dialog.dismiss();                      
        		    }  
        		});  
        		ad2.show(); 
    			
    		}
    		else
    		{
    	
    		Intent intent = new Intent(HelloAndroid2.this, HelloAndroid2.class);
            PendingIntent sender = PendingIntent.getBroadcast(HelloAndroid2.this,
                    0, intent, 0);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 5);

            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

              if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(HelloAndroid2.this, R.string.one_shot_scheduled,
                    Toast.LENGTH_LONG);
            mToast.show();
            
    		
    		AlertDialog ad = new AlertDialog.Builder(HelloAndroid2.this).create();  
        	ad.setCancelable(false); // This blocks the 'BACK' button  
    		ad.setMessage((CharSequence) editText.getText().toString());  
    		ad.setButton("OK", new DialogInterface.OnClickListener() {  
    		    public void onClick(DialogInterface dialog, int which) {  
    		        dialog.dismiss();                      
    		    }  
    		});
    		ad.show();
    		}
    	 
    		
    	}
    	});

What's the problem? I also tried in out on my phone, no luck. Ignore the alertdialogs, they are working fine, timing is the only thing that is not working.
 
Back
Top Bottom