Help. From Broadcastreceiver to MainActivity [closed]

1

Good day to the community, I need your help to solve a problem, I am a newbie. I have confused days and asking by this means if what I want can be done. I need to pass a value (state) from the broadcatReceiver to the Mainactivity and then perform other operations. The onResume method of the program does not meet the characteristics I want for the program and the getter methods do not work either.

What can I do? I need your help.

This is the program

//clase MusicIntentReceiver

public int getState() {
    return state;
}
@Override
public void onReceive(Context context, Intent intent)
{
    if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG))
    {           
        state = intent.getIntExtra("state", -1);

        switch (state)

        {
            case 0:

                Utilidades.mostrarToastText(context, "Headset is unplugged");
              ;
                break;
            case 1:
                Utilidades.mostrarToastText(context, "Headset is plugged");

                break;
            default:
                Utilidades.mostrarToastText(context, "I have no idea what the headset state is");


        }
    }
} 

The main class

Clase principal


public class MainActivity extends AppCompatActivity {


int state;


private MusicIntentReceiver myReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    myReceiver = new MusicIntentReceiver();


    state = myReceiver.getState();



    if (vibprueba==1) {

        txV= (TextView)findViewById(R.id.textocentro);
        txV.setText("Hola pude sacar Valor");


   }

} 

Here is a new question I asked about the same topic and I have not got a clear answer New question

Try also to apply this solution Passing data from a broadcastreceiver to an activity that is displayed in this forum, but getIntet () throws me the error "can not resolve method".

    
asked by Miguel_JGL 29.09.2017 в 20:57
source

1 answer

0

On the MainActivity

import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    JackPlugReceiver JackPlugRec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JackPlugRec = new JackPlugReceiver();

        IntentFilter intentFilter = new IntentFilter();

        intentFilter.addAction("android.intent.action.HEADSET_PLUG");

        registerReceiver(JackPlugRec, intentFilter);

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("miMsg"));

    }


  private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        String message = intent.getStringExtra("Mensaje");
        Log.d("XXX ", "Recibido : " + message);

      }
   };


    @Override
    protected void onDestroy() {
        if (JackPlugRec != null) {
            unregisterReceiver(JackPlugRec);
            JackPlugRec = null;
        }
     LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }




}

On BroadcastReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class JackPlugReceiver extends BroadcastReceiver {
    Context c;
        @Override
        public void onReceive(Context context, Intent intent) {

     c = context;

            if (!intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
                return;
            }

            boolean connectedHeadphones = (intent.getIntExtra("state", 0) == 1);
            boolean connectedMicrophone = (intent.getIntExtra("microphone", 0) == 1) && connectedHeadphones;
           // String headsetName = intent.getStringExtra("name");


                Log.v("XXX ", "onReceive");

        sendMessage();


    }

    private void sendMessage() {

         Intent intent = new Intent("miMsg");
         intent.putExtra("Mensaje", "Mensaje");
         LocalBroadcastManager.getInstance(c).sendBroadcast(intent);
    }
}

In manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gustavo.broadcastreceiver">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".JackPlugReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.HEADSET_PLUG" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true"></receiver>
    </application>

</manifest>

Running ... Greetings.

    
answered by 30.09.2017 / 15:25
source