Help with getter on android

0

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");


   }

} 
    
asked by Miguel_JGL 25.09.2017 в 16:22
source

1 answer

1

I think you need to define in the manifest, that your class behaves like a broadcastRecievers and that it should filter the action of connecting the headphones. I share the code.

<receiver android:name=".MusicIntentReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.HEADSET_PLUG" />
    </intent-filter>
</receiver>

In addition to that, I share this code where I believe you took the part that you have implemented. Pay special attention to the onResume () method since you must register to the receiver and onPause () you delete the subscription, and this is the best place to do it. Good luck

package com.example.testmbr;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class MainActivity extends Activity  {
private static final String TAG = "MainActivity";
private MusicIntentReceiver myReceiver;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myReceiver = new MusicIntentReceiver();
}

@Override public void onResume() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(myReceiver, filter);
    super.onResume();
}

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
            case 0:
                Log.d(TAG, "Headset is unplugged");
                break;
            case 1:
                Log.d(TAG, "Headset is plugged");
                break;
            default:
                Log.d(TAG, "I have no idea what the headset state is");
            }
        }
    }
}

@Override public void onPause() {
    unregisterReceiver(myReceiver);
    super.onPause();
}
}
    
answered by 25.09.2017 / 16:47
source