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.