Multiplos Broadcast Receiver

Asked

Viewed 118 times

0

I am writing an Android app and need to recognize various system status like connection , battery status and some other,

my doubt would be, how to create a function for that?

i created the following code block

IntentFilter iFilter_Bateria = new IntentFilter();
iFilter_Bateria.addAction(Intent.ACTION_BATTERY_CHANGED);  // pega a ação da bateria
registerReceiver(mBroadcast_Bateria, iFilter_Bateria); // registra o BroadCast no Android

IntentFilter iFilter_Status_Conexao = new IntentFilter();
registerReceiver(mBroadcast_Status_Conexao, iFilter_Status_Conexao); // registra o BroadCast no Android

IntentFilter iFilter_Status_Tela = new IntentFilter();
iFilter_Status_Tela.addAction(Intent.ACTION_SCREEN_ON); // pega a ação de ligar a tela
iFilter_Status_Tela.addAction(Intent.ACTION_SCREEN_OFF); // pega a ação de desligar a tela
registerReceiver(mBroadcast_Status_Tela, iFilter_Status_Tela); // registra o BroadCast no Android

IntentFilter iFilter_Usuario_Presente = new IntentFilter();
iFilter_Usuario_Presente.addAction(Intent.ACTION_USER_PRESENT); // pega a ação de usuario presente no aplicativo
registerReceiver(mBroadcast_Usuario_Presente, iFilter_Usuario_Presente);

but only the first broadcast and registered , I tested each function separately , and all worked right, but as I said, always the first is recorded, the other not, how do I solve this?

private BroadcastReceiver mBroadcast_Bateria = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("B. Bateria", "Registrado");
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;

        if (isCharging) {
            mTextCarregando.setText("Carregando");
        } else {
            mTextCarregando.setText("Não Carregando");
        }


        //////////////////////////////////////////////////////////////////////
        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        if (usbCharge) {
            mTextTipo.setText("Carregando Via USB");
        } else if (acCharge) {
            mTextTipo.setText("Carregando Via Tomada");
        } else {
            mTextTipo.setText("Não Carregando");
        }


        ///////////////////////////////////////////////////////////////////////


        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        mTextViewInfo.setText("Battery Scale : " + scale);

        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

        mTextViewInfo.setText(mTextViewInfo.getText() + "\nBattery Level : " + level);

        float percentage = level / (float) scale;

        mProgressStatus = (int) ((percentage) * 100);

        mTextViewPercentage.setText("" + mProgressStatus + "%");

        mTextViewInfo.setText(mTextViewInfo.getText() + "\nPercentage : " + mProgressStatus + "%");

        mProgressBar.setProgress(mProgressStatus);

    }

};


private BroadcastReceiver mBroadcast_Status_Conexao = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("B. Conexão", "Registrado");

        boolean wifi = Status_Conexao(context);

        if (wifi) {
            mTextWifi.setText("Conexão ok");
        } else {
            mTextWifi.setText("Sem Conexão");
        }

        ////////////////////////////////////////////////////////////////////////////////

        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

        //For 3G check
        boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        //For WiFi Check
        boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();

        if (isWifi) {
            mTextWifiTipo.setText("Via Wifi");
        } else if (is3g) {
            mTextWifiTipo.setText("Via 3G");
        } else {
            mTextWifiTipo.setText("Sem Conexão");
        }

    }

    private boolean Status_Conexao(Context context) {
        boolean connect;
        ConnectivityManager connMgr = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            connect = true;
        } else {
            connect = false;
        }
        return connect;
    }


};


private BroadcastReceiver mBroadcast_Status_Tela = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("B. Tela", "Registrado");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            mTextTelaLigada.setText("Tela Desligada");
            chronometer.stop();
            chronometer.setText("00:00");
            TelaLigada = false;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            mTextTelaLigada.setText("A tela esta em uso");
            chronometer.start();
            TelaLigada = true;
        }

    }
};

private BroadcastReceiver mBroadcast_Usuario_Presente = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("B. Usuario", "Registrado");

        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
            UsuarioPresente = true;
            mUsuarioPresent.setText("Usuario Presente");
        } else
            mUsuarioPresent.setText("Não Presente");

    }

};
  • How do you know that they are not all registered? The problem will not be elsewhere in the code?

  • Can you post all the code? I believe it might be on your Broadcasts

  • @ramaral , I call a log for each recorded broadcast

  • @Andersoncanteiro added the code for you.

  • When testing one at a time also uses a class field and makes the assignment at the time of declaration(private BroadcastReceiver mBroadcast_Bateria = new BroadcastReceiver())? It seems to me that the attribution at the time of the declaration will not work because at least the references to the views are null.

  • It wouldn’t be better for you to do them all separately by giving Broadcastreceiver extensions and registering them all in the manifest, along with Intent-filter ?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.