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".