Good day to the community. I am a novice in programming and I need your help
I am working with a simple example of android downloaded from the web, it indicates if a hearing aid is connected to the phone through a quick message. My idea is to take advantage of the state value of the MusicIntentReceiver class and use it initially to place a message but from the main class.
The application is installed on the phone correctly and opens but does not display the message "Hi, I could take value". The activity-main file is fine.
I have been researching these days on the web but I have not solved it. The state variable is in private. private int state;
What am I doing wrong? Thanks.
//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");
}
}
}
}
Main class
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");
}
}