1. Are you ready for the Galaxy S20? Here is everything we know so far!

Scheduled Runs

Discussion in 'Android Development' started by Crowbarella, Sep 12, 2019.

  1. Crowbarella

    Crowbarella Lurker
    Thread Starter

    I'm trying to schedule the running of a function in the MainActivity thread. I've tried both an AlarmManager and a Handler. They both look like they're going through, but nothing ever runs at the time it's supposed to. Not sure what I'm doing wrong here. Any help would be greatly appreciated. Admittedly, I'm a bit noobish, though this is my second app (both very basic).

    The relevant sections of the code for Handler:
    Code (Java):
    1.     public Runnable r;
    2.     public Handler TSH = new Handler();
    3.  
    4.     @Override
    5.     protected void onCreate(Bundle savedInstanceState) {
    6. ...
    7.         r = new Runnable() {
    8.             @Override
    9.             public void run() {         Log.d("Tag","Hi Again");
    10.                 RefreshList();}
    11.         };
    12. Calendar c1 = Calendar.getInstance();
    13. c1.add(Calendar.MINUTE,1);
    14. TSH.postAtTime(r, c1.getTimeInMillis());
    The relevant sections of the code for AlarmManager:
    Code (Java):
    1.  
    2. public class TrainCheckerAlarm  extends BroadcastReceiver {
    3.     public MainActivity ma;
    4.  
    5.     public void GetMA(MainActivity m) {ma = m;}
    6.  
    7.     @Override
    8.     public void onReceive(Context context, Intent intent)
    9.     {
    10.         ma.RefreshList();
    11.     }
    12.     public void setAlarm(Context context, Calendar cal)
    13.     {
    14.         AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    15.         Intent i = new Intent(context, TrainCheckerAlarm.class);
    16.         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    17.         am.set(AlarmManager.ELAPSED_REALTIME, cal.getTimeInMillis(), pi);
    18.     }
    19. }
    20.  
    Then in MainActivity, there's a:
    public TrainCheckerAlarm TCA = new TrainCheckerAlarm();
    TCA.GetMA(this);
    TCA.setAlarm(this,c1);
     


Loading...
Similar Threads - Scheduled Runs
  1. kwhopson
    Replies:
    1
    Views:
    198
  2. App Update
    Replies:
    0
    Views:
    299
  3. chethan1703
    Replies:
    0
    Views:
    534
  4. chethan1703
    Replies:
    1
    Views:
    269
  5. kwhopson
    Replies:
    5
    Views:
    1,206
  6. Clifton E Bean
    Replies:
    3
    Views:
    299
  7. Vlad2
    Replies:
    7
    Views:
    469
  8. mk2019
    Replies:
    3
    Views:
    355
  9. MathewR
    Replies:
    1
    Views:
    344
  10. wcndiana
    Replies:
    5
    Views:
    308

Share This Page

Loading...