how can I get rid of the compile error?
sendBroadcast(intent);
this method works well in another app that I made, however it has a compile error in the current app. the red underline has the eror message “undefined for the type new Runnable”
sendBroadcast() is part of the Context class, not the Intent class.
public class Server { // external class
public class ServerThread implements Runnable { // nested class
public void run() {
try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus = "Listening on IP: " + SERVERIP;
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true) {
// listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction("com.example.AudioPlay");
intent.putExtra("serverStatus","Connected");
sendBroadcast(intent);
}
});
<<<< EDIT >>>>
I changed the focus of the question to the compile error. The method works fine in the other app. It is in this particular situation where I am puzzled about the compile error.
for accessing
sendBroadcastmethod in a class which is not extending Activity, service or other Application components you will need to pass component context to it using class constructor as :and pass context like from Activity as :