• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Using processing-core library with alarmmanager

Richardy

Lurker
Aug 25, 2021
1
0
Hi,
I am trying to get some some processing code via the processing-core library to to be implemented from the broadcast receiver in some alarm manager code in Android Studio.

I started by creating a simple alarm manger 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 code but i am unsure as to how i do this.

My attempt at doing this is shown below. The only class that creates errors is the broadcast receiver class which creates errors related to frameLayout, setContentView and Fragment.setView.

Any help with this is much appreciated.

Cheers.

All classes and XML file are shown below:

MainActivity.java
Code:
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
Code:
<?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
Code:
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
Code:
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;
    }
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones