In the MainActivity I need to implement a listener, which can receive data (boolean statusWIFIX) sent by TcpClient, The TcpClient class is being used in MainActivity, that is, it is instantiated there,
This is my code of my TcpClient:
public class TcpClient {
.
.
public Boolean statusWIFIX = false;
private TcpListener1 mTcpListener1;
public TcpClient(TcpListener1 tcpListener1) {mTcpListener1 = tcpListener1;}
interface TcpListener1 {
void onReportStatusWIFIX(boolean statusWIFIX);
}
public void stopClient() {
statusWIFIX = false;
// Report status to listener (MainActivity)
mTcpListener1.onReportStatusWIFIX(statusWIFIX);
Log.e( "DEBUG-->", "Coneccion Cerrada: "+ statusWIFIX );
sendMessage(Constants.CLOSED_CONNECTION+": " + Modelox); // send message that we are closing the connection
}
.
.
And this is the code of my MainActivity, where it evaluated the value of statusWIFI which I will use to hide or make visible an icon on the upper screen:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
...
private TcpClient mTcpClient;
private TcpClient.TcpListener1 mTcpListener1 = new TcpClient.TcpListener1()
{
@Override
void onReportStatusWIFIX(boolean statusWIFIX) {
if (statusWIFIX == true) {
statusWIFI = true;
}else{statusWIFI = false;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
mTcpClient = new TcpClient(mTcpListener1);
...
}
...
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem IconWIFI = menu.findItem(R.id.bt1_wifi);
if (statusWIFI == true){
IconWIFI.setVisible(true);
}
if (statusWIFI == false){
IconWIFI.setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
...
}
But I have this error, could someone tell me how to correct it.
In the case in which your class does not extend one
Activity
orFragment
what is done is to modify the constructor of the class to send the context, in this way you could use it inside the class:I don't quite understand your code, rather than trying.
The error is that the intent is not being initialized with the correct parameters:
Apparently your TcpClient class doesn't extend Context so you can't use this as the first parameter.
But I'm not sure what you're doing is what you want to do: I see that the code wants to OPEN a new activity of type MainActivity by passing it some parameters, and also WAITS for MainActivity to send information back. This will not pass information to an existing activity.
Is this what you really want? If so, then pass an object of type Context in the first parameter of the intent and that's it.
If not then there are other options.
I imagine that TcpClient is being used in MainActivity, that is, it is instantiated there, if this is correct there are other ways to pass information.
If you absolutely want to use intents to pass information, MainActivity needs to implement a BroadcastReceiver that can receive intents sent by TcpClient.
Example Listener
Interface TcpListener:
TcpClient:
MainActivity: