Hi,
I am new to this forum and also new to android programming using Android Studio and am stuck on a coding problem so i am not sure this is the right forum but any help is much appreciated:
I am trying to get some some code via the processing-core library (processing.org) to be implemented from the broadcast receiver using the alarm manager in Android Studio.
I started by creating a simple alarm manager App that fires an alarm every minute (user settable) and this works fine. I then created an App that makes use of the processing-core library (imported the library as a jar dependency into android studio) and this also works.
The next step was to amalgamate the above and create an App that fires a sketch class from the broadcast receiver that includes some simple processing library code but i am unsure as to how i do this.
My attempt at doing this is shown below. Android studio only finds errors in the Broadcast reciever class (MyBroadcastReceiver.java) which indicates that the way i am attmpting to fire the sketch.java code is incorrect. The errors are related to frameLayout, setContentView and Fragment.setView.
Any help with this or suggestions are much appreciated.
Cheers.
All classes and XML file are shown below:
public class MainActivity extends AppCompatActivity {
private PApplet sketch;
Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start= findViewById(R.id.button);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startAlert();
}
});
}
public void startAlert(){
EditText text = findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alrmmngrplusprocsktch.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="103dp" />
<EditText
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:ems="10" />
</RelativeLayout>
MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
private PApplet sketch;
@Override
public void onReceive(Context context, Intent intent) {
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);
}
}
Sketch.java
import processing.core.PApplet;
public class Sketch extends PApplet
{
int x = 0;
double h, h2;
public void settings()
{
fullScreen();
}
public void setup()
{
background(0);
noStroke();
fill(102);
}
public void draw()
{
//background(0);
h = height*0.2;
h2 = height*0.6;
rect(x, (float)h, 1, (float)h2);
x = x + 2;
}
}