can't register broadcast receiver in processing 3 ?

edited February 2017 in Android Mode

Hi.

a simple code to check the battery state worked in processing 2.

it doesn't work anymore in processing 3

(my issue is I can't reinstall android mode in processing 2, the android mode doesn't appear in mode manager).

is this a known bug ?

here's the code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

int level=0;
int charging;
int voltage;
import android.os.BatteryManager;
int temp;
String tech;
void setup()
{
  battery_checking();
}
void draw()
{

}


void battery_checking()
{
  println("======================");
  println("Niveau Batterie :"+level+"% ");
  //  println("Technologie : "+tech);
  //  println("Voltage : "+voltage);
  //  println("Temperature : "+temp);
  //  println("plugged : "+charging);
  println("======================");
}


private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {

  @ Override
    public void onReceive(Context c, Intent i) {


    level = i.getIntExtra("level", 0);
    voltage =  i.getIntExtra("voltage", 0);
    charging = i.getIntExtra("plugged", 0);
    tech = i.getStringExtra("technology");
    temp =  i.getIntExtra("temperature", 0);
  }
};

@ Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

thanks in advance !

Answers

  • it doesn't work anymore in processing 3

    In what way doesn't it work? Does it print errors? Does it just not return a value?

    Android version?

  • I try with android 7.1 and 6. processing gives me: "the function registerReceiver(BroadcastReceiver, IntentFilter)" doesn't exist...

  • edited February 2017

    @pauline909=== 2== you are in an Activity; 3== you are in a fragment; i think this is your problem (not tested!!!, guessed!!!) i verify Asap

  • Answer ✓

    I made some changes to your code. Check them out,

    Kf

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    
    import android.os.BatteryManager;
    import android.os.Looper;
    import android.app.Activity;
    import android.app.Fragment;
    
    
    Activity act;
    Context mc;
    
    int level=0;
    int charging;
    int voltage;
    int temp;
    String tech;
    
    
    void setup()
    {
      fullScreen();
      battery_checking();
      textSize(28);
      textAlign(CENTER,CENTER);
      fill(225);
    }
    
    void draw()
    {
      background(25,25,220);
      text("Battery level: " +level+
      "%\nVoltage "+voltage+
      "\nCharging "+charging+
      "\nTechnology "+tech+
      "\nTemperature "+temp,
      width/2,height/2);
    }
    
    void battery_checking()
    {
      println("======================");
      println("Niveau Batterie :"+level+"% ");
      //  println("Technologie : "+tech);
      //  println("Voltage : "+voltage);
      //  println("Temperature : "+temp);
      //  println("plugged : "+charging);
      println("======================");
    }
    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    
      @ Override
        public void onReceive(Context c, Intent i) {
    
    
        level = i.getIntExtra("level", 0);
        voltage =  i.getIntExtra("voltage", 0);
        charging = i.getIntExtra("plugged", 0);
        tech = i.getStringExtra("technology");
        temp =  i.getIntExtra("temperature", 0);
      }
    };
    
    @ Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      act = getActivity();
      mc = act.getApplicationContext();
      mc.registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }
    
  • cool !!!! it does work, thanks !!!

  • @pauline909=== what kfrager added (lines 70_72) is the way to write code in a fragment...

Sign In or Register to comment.