user2495899
user2495899

Reputation:

Call A Method From a different Class JAVA

I have this method called "createAlarms()" which is on initial app setup, sets an alarm for a specific time. On the running of this alarm, it creates a notification which I am creating using a Broadcast Receiver Class.

public void createAlarms() {
    cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, alarmintervalint);
    calintent = new Intent(this, AlarmBroadcastReceiver.class);
    calpendingintent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345, calintent, 0);
    am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, alarmintervalint, calpendingintent);
}

I want this alarm to repeat every "alarmintervalint" time period. I could do this by using the "am.setRepeating()" function, but my problem is a bit more complicated then that. After sending a certain amount of alarms (such as 50. will be calculated by the program), I want all the values to change, so that the alarmintervalint will change.

private void showNotification(Context context) {
    PendingIntent notifpi = PendingIntent.getActivity(context, 0, new Intent(context, Main.class), 0);

    NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Hello!")
        .setContentText("Welcome!")
        .addAction(R.drawable.ic_launcher, "Open App", notifpi);
    mBuilder.setContentIntent(notifpi);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
    System.out.println("6");

}

For example, right now, an alarm goes off every 2 hours, and I get a notification every 2 hours. After getting 50 notifications, I want to set the Alarm to go off after 3 hours. And again after 50 notifications, make it 4 hours. (This is just an example, it will be a bit more complicated.

How would I do this? Right now what I think is to have some sort of counter in my broadcastreceiver class, and after the counter reaches 50 (in this example's case), it will call upon the createAlarms() class and change the timings and stuff. Would this work?

Upvotes: 0

Views: 178

Answers (2)

Father Stack
Father Stack

Reputation: 524

I'm not totally sure what you're using the alarm for, but it looks like you want people to gradually smoke less often.
In that case I'd like to add to the previous answers, that you might want to persist your last used interval in case your app crashes or the phone is rebooted. You can easily archieve that by using SharedPreferences.

Upvotes: 0

Zaw Than oo
Zaw Than oo

Reputation: 9935

Simplest way is to use the Timer. Set TimerTask in your initial setup method, main method or whatever.

Example :

import java.util.TimerTask;

public class Schedular extends TimerTask {
    @Override
    public void run() {
        //your implementation.
        System.out.println("Run Me ~");
    }
}

import java.util.Timer;
import java.util.TimerTask;

public class Main{
    public static void main(String[] args){

        TimerTask task = new Schedular();
        Timer timer = new Timer();
        //miliseconds
        timer.schedule(task, 1000, 60000);
    }
}

For Ref : Timer

Upvotes: 1

Related Questions