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

Using processing-core library with alarmmanager

Discussion in 'Android Development' started by Richardy, Aug 26, 2021.

  1. Richardy

    Richardy Lurker
    Thread Starter

    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 (Text):
    1. public class MainActivity extends AppCompatActivity {
    2.     private PApplet sketch;
    3.     Button start;
    4.  
    5.     @Override
    6.     protected void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.activity_main);
    9.         start= findViewById(R.id.button);
    10.  
    11.         start.setOnClickListener(new View.OnClickListener() {
    12.             @Override
    13.             public void onClick(View view) {
    14.                 startAlert();
    15.             }
    16.         });
    17.     }
    18.  
    19.     public void startAlert(){
    20.         EditText text = findViewById(R.id.time);
    21.         int i = Integer.parseInt(text.getText().toString());
    22.         Intent intent = new Intent(this, MyBroadcastReceiver.class);
    23.         PendingIntent pendingIntent = PendingIntent.getBroadcast(
    24.                 this.getApplicationContext(), 234324243, intent, 0);
    25.         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    26.         alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
    27.                 + (i * 1000), pendingIntent);
    28.         Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
    29.     }
    30. }
    Activity_main.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:app="http://schemas.android.com/apk/res-auto"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     tools:context="com.example.alrmmngrplusprocsktch.MainActivity">
    8.  
    9.     <Button
    10.         android:id="@+id/button"
    11.         android:layout_width="wrap_content"
    12.         android:layout_height="wrap_content"
    13.         android:text="Start"
    14.         android:layout_alignParentBottom="true"
    15.         android:layout_centerHorizontal="true"
    16.         android:layout_marginBottom="103dp" />
    17.  
    18.     <EditText
    19.         android:id="@+id/time"
    20.         android:layout_width="wrap_content"
    21.         android:layout_height="wrap_content"
    22.         android:layout_alignParentTop="true"
    23.         android:layout_centerHorizontal="true"
    24.         android:layout_marginTop="22dp"
    25.         android:ems="10" />
    26. </RelativeLayout>
    MyBroadcastReceiver.java
    Code (Text):
    1. public class MyBroadcastReceiver extends BroadcastReceiver {
    2.     private PApplet sketch;
    3.    
    4.     @Override
    5.     public void onReceive(Context context, Intent intent) {
    6.         FrameLayout frame = new FrameLayout(this);
    7.         frame.setId(CompatUtils.getUniqueViewId());
    8.         setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
    9.                 ViewGroup.LayoutParams.MATCH_PARENT));
    10.  
    11.         sketch = new Sketch();
    12.         PFragment fragment = new PFragment(sketch);
    13.         fragment.setView(frame, this);
    14.     }
    15. }
    Sketch.java
    Code (Text):
    1. import processing.core.PApplet;
    2.  
    3. public class Sketch extends PApplet
    4. {
    5.     int x = 0;
    6.     double h, h2;
    7.  
    8.     public void settings()
    9.     {
    10.         fullScreen();
    11.     }
    12.  
    13.     public void setup()
    14.     {
    15.         background(0);
    16.         noStroke();
    17.         fill(102);
    18.     }
    19.  
    20.     public void draw()
    21.     {
    22.         //background(0);
    23.  
    24.         h = height*0.2;
    25.         h2 = height*0.6;
    26.         rect(x, (float)h, 1, (float)h2);
    27.         x = x + 2;
    28.     }
    29. }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Using processing core
  1. Marvin P Droid
    Replies:
    17
    Views:
    230
  2. Gobshyte
    Replies:
    1
    Views:
    224
  3. DridriLaBastos
    Replies:
    0
    Views:
    228
  4. npuleio
    Replies:
    0
    Views:
    428
  5. BabdevPro
    Replies:
    0
    Views:
    291
  6. Drchiefd113
    Replies:
    5
    Views:
    454
  7. Hada de la Luna
    Replies:
    1
    Views:
    457
  8. acenspades
    Replies:
    0
    Views:
    473
  9. mysorian
    Replies:
    0
    Views:
    421
  10. Hornetzero
    Replies:
    5
    Views:
    555

Share This Page

Loading...