Can Android mode do timelapse image capture and FTP upload

The myBroadcastReceiver extends another class, so unless that class has an available setContentView method then yes you would need to create one inside your class.

As for the context error you may have to grab the context within the sketch and pass it into thee method. I forget the exact way to do this, but I can provide some code shortly you could test.

normally to retrieve context, you need to add the following import statements

import android.app.Activity;
import android.content.Context;

create the following objects

Activity act;
Context ctx;

then in code

act = this.getActivity();
ctx = act.getApplicationContext();

HI,
Thanks for the reply and the code example, i will work on this.

What i find odd with the current code is that if i take the code that doesn’t work from myBroadcastReceiver.java and put it into MainActivity.java;

public class MainActivity extends AppCompatActivity
{
    private PApplet sketch;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        FrameLayout frame = new FrameLayout(this);
        frame.setId(CompatUtils.getUniqueViewId());
        setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        sketch = new Sketch();
        PFragment fragment = new PFragment(sketch);
        fragment.setView(frame, this);

        startInterval();
    }

It compiles with no errors and i can install it on the phone successfully and it will fire the sketch.java code initially and the altered broadcastreciver;

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        System.out.println("----------ALARM----------");

        Intent serviceIntent = new Intent(context, Sketch.class);
        context.startService(serviceIntent);
}

will detect the alarm and print ‘-----ALARM-----’ to the console but does not run the sketch.java code.

I think my lack of understanding stems from the fact that i am used to programming micro-controllers (arduino, pyboard etc) so its just programming bare metal which seems a lot easier.

For example the following processing code fails with the error ‘The function “getSystemService(String)” does not exist’ which i do not understand but all i really want the code to do is wake up the processor from sleep and continue to run the code in draw() until the system automatically goes into sleep again (say 30s) and repeat this indefinitely which can be easily achieved with a micro-controller;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

Context context;
Intent intent;
PendingIntent pendingIntent;
AlarmManager alarmManager;
boolean alarmSet = false;
String currentTime="";

void setup()
{
  orientation(LANDSCAPE);
  background(0);
  textSize(50);
}

void draw()
{
  background(0);
  currentTime = year() + "-" + month() + "-" + day() + "  " + hour() + ":" + minute() + ":" + second();
  println ("current time> " + currentTime);
  text("Current Time: "+ currentTime, 0, 200);
  delay(1000);
  if (alarmSet == false) 
  {
    startAlert();
    alarmSet = true;
  }
}

  void startAlert()
  {
    long repeatInterval = 60 * 1000;
    long triggerTime = SystemClock.elapsedRealtime() + repeatInterval;
    intent = new Intent(this.getActivity(), MyBroadcastReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(
            this.getActivity().getApplicationContext(), 234324243, intent, 0);
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
            + (60 * 1000), pendingIntent);  //60 secs apart
  }
        
public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {     
       alarmSet = false;
    }
}

Again, your help with this is much appreciated.

Cheers.

Hi,
Quick update
I fixed the error related to ‘The function “getSystemService(String)” does not exist’ - thanks to your example code for that.

The code compiles and installs successfully on the phone however the alarm does not seem to trigger a resumption of the draw() code.

Any help is much appreciated.

Cheers.

Do you have the updated code?

Hi,
Yep the updated code is below;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

Context context;
Intent intent;
Activity act;
PendingIntent pendingIntent;
AlarmManager alarmManager;
boolean alarmSet = false;
String currentTime="";

void setup()
{
  orientation(LANDSCAPE);
  background(0);
  textSize(50);

  act = this.getActivity();
  context = act.getApplicationContext();
}

void draw()
{
  background(0);
  println(alarmSet);
  currentTime = year() + "-" + month() + "-" + day() + "  " + hour() + ":" + minute() + ":" + second();
  println ("current time> " + currentTime);
  text("Current Time: "+ currentTime, 0, 200);
  delay(1000);
  if (alarmSet == false) 
  {
    startAlert();
    alarmSet = true;
  }
}

  void startAlert()
  {
    intent = new Intent(this.getActivity(), MyBroadcastReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(context, 234324243, intent, 0);
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
            + (60 * 1000), pendingIntent);  //60 sec delay
  }
        
public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {     
       alarmSet = false;
       println(alarmSet);
    }
}

Another thing that i dont understnad is the value 234324243 in

pendingIntent = PendingIntent.getBroadcast(context, 234324243, intent, 0);

I have seen other example code that has the value 0 instead but it doesn’t seem to make any difference.

Cheers.

So just to clarify you need the application to keep printing even when the screen is locked for example?

Just to clarify I’m just looking through some examples on stack and trying to perhaps come up with an alternative. It seems to me that using a service would probably be the best option.

also you may want to take a look at this thread

I have managed to come up with a bit of a hacky solution.

it does throw an error if the app is loaded into the phone whilst the phone is on standby, but it does at least keep printing whilst the phone is on standby.

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

Context context;
Intent intent;
Activity act;
import android.app.Activity;
PendingIntent pendingIntent;
AlarmManager alarmManager;
boolean alarmSet = false;
String currentTime="";

void setup() {
  orientation(LANDSCAPE);
  background(0);
  textSize(50);

  act = this.getActivity();
  context = act.getApplicationContext();
}

void draw() {
  background(0);
  println(alarmSet);
  currentTime = year() + "-" + month() + "-" + day() + "  " + hour() + ":" + minute() + ":" + second();
  println ("current time> " + currentTime);
  text("Current Time: "+ currentTime, 0, 200);
  delay(1000);
  if (alarmSet == false) 
  {
    startAlert();
    //alarmSet = true;
  }
}

void startAlert() {
  intent = new Intent(this.getActivity(), mbr.class);
  pendingIntent = PendingIntent.getBroadcast(context, 234324243, intent, 0);
  alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
    + (60 * 1000), pendingIntent);  //60 sec delay
    println("pause");
    alarmSet = false;
    draw();
}

class mbr extends BroadcastReceiver {
  @Override
    public void onReceive(Context context, Intent intent)
  {     
    alarmSet = false;
    println(alarmSet+", ");
  }
}

@Override
  public void onPause() {
  super.onPause();  // Always call the superclass method first
  startAlert();
}

@Override
  public void onResume() {
  super.onResume();  // Always call the superclass method first
  alarmSet = true;
}

```un

Hi, and thanks for the reply and the example code.

Ahh no, not really - when the phone goes into standby (after 30s in this case) - whatever was happening can stop to preserve battery but when the alert is received by the broadcast receiver i need whatever was running to resume i.e. the display should turn back on and print the date and time on it until the phone goes into standby again.

So in a nutshell when a running App pauses and goes into standby, the App should resume after an alert is received.

I ran your code on my phone and it did indeed keep reporting the time (to the console only) when the screen went off (App paused for standby) but it didn’t display the time on the phone display before this (all i got was a white screen) and did not turn the screen back on when the alert was received. Possibly due to my phone setup?

I had actually placed a a similar question related to this on stack a while ago and never got a reply although i think that may have been because it down down-voted, so i gave up but i might have another go and see what happens.

I will continue to experiment.

Again thanks for all your continued help with this.

Cheers.

Il take another look when I have a moment.

HI,
And thanks for the reply.

I have found a couple of other potential options:

  1. it is possible to use the recreate() method (setup within the main activity) and called from within the BroadcastReceiver this has the effect of running the App through its life-cycle to onDestroy(), killing hte App and then recreating it, which isn’t ideal but in theory would restart the App after receiving an alarm, unfortunately it didnt seem to work although it could have been the way i implemented it in Android Studio.
  2. There is an interesting article here which seems to indicate that the broadcast receiver needs to be registered within the main activity. The article doesn’t. deal with alarms specifically but is related. I haven’t tried the approach yet as i need time to work though it to understand whats going on.

Cheers.

Hi,
Update; it looks this might be the way to go.

Cheers.

I’m looking at further examples on github, bear in mind im very new to android java myself, and mostly use solutions which others have come up with.

Here are some examples I think might help

this is written in kotlin but I think it shouldnt be too difficult to translate to java and also should still be able to help with grasping the basic logic required

this is a complete alarm manager a long read for sure but might be of interest to some

snippets

Again give me a bit of time, I have other projects I want to finish too but this is on my to do list as I am also interested in this.

HI,
And thanks for the reply.

And many thanks for the code examples. I will work my way through them.

Cheers.