简体   繁体   English

从其他类JAVA调用方法

[英]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. 我有一个名为“ createAlarms()”的方法,该方法在初始应用程序安装时就设置了特定时间的警报。 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. 我希望此警报重复每个“ alarmintervalint”时间段。 I could do this by using the "am.setRepeating()" function, but my problem is a bit more complicated then that. 我可以通过使用“ am.setRepeating()”函数来做到这一点,但是我的问题要复杂得多。 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. 发送一定数量的警报(例如,将由程序计算出50个警报)后,我希望所有值都发生变化,以使alarmintervalint发生变化。

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. 例如,现在,警报每2小时响一次,我每2小时收到一条通知。 After getting 50 notifications, I want to set the Alarm to go off after 3 hours. 收到50条通知后,我想将闹钟设置为3小时后关闭。 And again after 50 notifications, make it 4 hours. 然后在收到50条通知后,将其设为4小时。 (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. 现在,我认为在我的broadcastreceiver类中有一个计数器,在计数器达到50(在本示例中为例)之后,它将调用createAlarms()类并更改时间和内容。 Would this work? 这行得通吗?

Simplest way is to use the Timer . 最简单的方法是使用Timer Set TimerTask in your initial setup method, main method or whatever. 在初始设置方法,主方法或任何其他方法中设置TimerTask

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 对于参考: 计时器

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 . 您可以使用SharedPreferences轻松归档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM