Search code examples
androidonclickalarm

How to Call multiple methods on a single button click in android ?`


I implement so many methods but didn't get required results. I want to call 2 methods on a single click button in Android.

I try the below code but it is not work for me.

 @Override
public void onClick(View v) {
 // TODO Auto-generated method stub
 switch (v.getId()) {

 case R.id.savebtn:   

   startAlert(); 

 default:
  break;
 }

}


 public void startAlert() {          

        EditText Start_alarm = (EditText) findViewById(R.id.timeout_et);  
        int i = Integer.parseInt(Start_alarm.getText().toString());  

        Intent intent = new Intent(this, MyBroadcastReceiver.class);  
        PendingIntent pendingIntent = PendingIntent.getBroadcast(  
                                      this.getApplicationContext(), 234324243, intent, 0);  
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                                      + ((i * 1000)*60), pendingIntent);  
        Toast.makeText(this, "Alarm set in " + i + " minute",Toast.LENGTH_LONG).show();  

        // App Exit here
        Intent intent_exit = new Intent(Intent.ACTION_MAIN);
        intent_exit.addCategory(Intent.CATEGORY_HOME);
        intent_exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent_exit);
        finish();

        SetUpdateInterval();

    }

    //-------Repeating Video--------- 
 private void SetUpdateInterval() {
    // TODO Auto-generated method stub
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
     Intent intent = new Intent(this, MyBroadcastReceiver.class);  

     PendingIntent pendingIntent = PendingIntent.getBroadcast(  
             this.getApplicationContext(), 234324243, intent, 0); 
     EditText update_time = (EditText) findViewById(R.id.updateinterval_et); 
     int j = Integer.parseInt(update_time.getText().toString());  
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(System.currentTimeMillis());          
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * j, pendingIntent);

}

Now, with this code only one method is call, i.e StartAlert(). I also want to call SetUpdateInterval method. So can anybody tell me, how cam i run these two methods simultaneously.

Thankx in advance.

Simplified code

    @Override
public void onClick(View v) {
 // TODO Auto-generated method stub
 switch (v.getId()) {   
 case R.id.savebtn:

   startAlert(); 
   SetUpdateInterval();

    finish();            
     break;

      case R.id.cancelbtn:
  finish();
        break;

 default:
  break;
 }
}

 public void startAlert() {  

        int i = Integer.parseInt(timeout_et.getText().toString());  
        System.out.println("VAlue of i" +" "+i);

        int j = Integer.parseInt(update_et.getText().toString());  

        Intent intent = new Intent(this, MyBroadcastReceiver.class);  
        PendingIntent pendingIntent = PendingIntent.getBroadcast(  
                                      this.getApplicationContext(), 234324243, intent, 0);  
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                                      + ((i * 1000)*60), pendingIntent); 
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                + ((j * 1000)*60), j, pendingIntent); 
        Toast.makeText(this, "Alarm set in " + i + " minute",Toast.LENGTH_LONG).show();  

      }

   //-------Repeating Video--------- 
     private void SetUpdateInterval() {

         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
         Intent intent = new Intent(this, MyBroadcastReceiver.class);  

         PendingIntent pendingIntent = PendingIntent.getBroadcast(  
                 this.getApplicationContext(), 234324243, intent, 0); 
         EditText update_time = (EditText) findViewById(R.id.updateinterval_et); 
         int j = Integer.parseInt(update_time.getText().toString());  
         Calendar calendar = Calendar.getInstance();
         calendar.setTimeInMillis(System.currentTimeMillis());          
         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * j, pendingIntent);

       // App Exit here
       Intent intent_exit = new Intent(Intent.ACTION_MAIN);
       intent_exit.addCategory(Intent.CATEGORY_HOME);
       intent_exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent_exit);
       finish();

 }

Solution

  • Just replace below code, you are finishing activity before calling that method :

        @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     switch (v.getId()) {
    
     case R.id.savebtn:   
    
       startAlert(); 
       SetUpdateInterval()
    
     default:
      break;
     }
    
    }
    

    //Put below code in SetUpdateInterval() method for some delay and then do finish.

    new Timer().schedule(new TimerTask() {
    
        @Override
        public void run() {
            finish();
    
        }
    },3000);