Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5944833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:35:45+00:00 2026-05-22T16:35:45+00:00

Android: How can i show a toast from a thread running in a remote

  • 0

Android: How can i show a toast from a thread running in a remote service? Whenever i run from a different thread in a service, the app crashes…

public class BroadcastService extends Service {

private static final String TAG = BroadcastService.class.getSimpleName();
private MessageFormatter mFormatter = new MessageFormatter();
private BroadcastComm broadCastComm = new BroadcastComm();
private Task commTask = new Task();
private volatile static boolean stopBroadcastRequested = false;
private volatile static boolean isSocketOpen = false;
private volatile static byte[] messageToBeSent;
private Handler serviceHandler;
private ConnectivityStatus connStatus;


@Override
public void onCreate() {
    super.onCreate();
    Log.e(TAG, "Service creating");
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    serviceHandler = new Handler();
    serviceHandler.post(commTask);
    stopBroadcastRequested = false;
    messageToBeSent = mFormatter.formBroadCastMessage("GET_PERIPH_DATA");
}


class Task implements Runnable{

    @Override
    public void run() {
        while(!stopBroadcastRequested){
                           Toast.makeText(context, "SSSSSSSSSS", 5).show();
            Log.i(TAG, "Thread Task started");          
            try {
                Log.d("SERVICE CLASS", "STARTED THREAD - Writing in output stream");
                isSocketOpen = broadCastComm.isAliveOrOpenSocket("192.168.43.2", 6000, 17, 0);

                if(isSocketOpen){
                    OutputStream outStream = broadCastComm.getCurrentOutputStream();

                    outStream.write(messageToBeSent);
                    if(Integer.valueOf(messageToBeSent[2]) != (byte)0xA0){
                        Log.e("REVERTING", "REVERTING");
                        messageToBeSent = mFormatter.formBroadCastMessage("GET_PERIPH_DATA");
                    }

                    Log.d("OUTPUT STREAM", "Message sent ->" + ByteArrayToString(messageToBeSent));
                }

                Thread.sleep(3000L);
                if(isSocketOpen){
                    Log.d("SERVICE CLASS", "Started input");
                    InputStream inStream = broadCastComm.getCurrentInputStream();
                    BufferedInputStream buf = new BufferedInputStream(inStream);
                    byte[] buffer;

                    while(buf.available() > 0){
                        Log.d("SERVICE CLASS", "Input available");
                        int size = buf.available();
                        buffer = new byte[size];
                        inStream.read(buffer, 0, size);
                        if(buffer[2] == (byte)0xA0){
                            BroadcastPacket packet = broadCastComm.decodeSocketData(buffer);
                            synchronized (incomingPacketLock) {
                                latestBroadcastPacket = packet;
                            }
                            synchronized (listeners) {
                                for (BroadcastListener listener : listeners) {
                                    try {
                                        listener.handlePacketsUpdated();
                                    } catch (RemoteException e) {
                                        Log.w(TAG, "Failed to notify listener " + listener, e);
                                    }
                                }
                            }
                        }
                    }
                }

            } catch (Throwable t) { 
                Log.e(TAG, "Failed to retrieve data in thread", t);
            }
            Log.d("SERVICE CLASS", "End of THREAD");

        }
        if(stopBroadcastRequested){
            Log.e("SERVICE", "STOPPED THREAD");
        }
    }

    public synchronized void stopThread(){
        stopBroadcastRequested = true;
    }

}

     ...........

@Override
public IBinder onBind(Intent intent) {
    if (BroadcastService.class.getName().equals(intent.getAction())) {
        Log.d(TAG, "Bound by intent " + intent);
        return apiEndpoint;
    } else {
        return null;
    }
}


   .........

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(TAG, "Service destroying");
    stopBroadcastRequested = true;
    broadCastComm.clearConnections();
    try {
        serviceHandler.removeCallbacks(commTask);
        serviceHandler = null;

    } catch (Exception e) {
        Log.d("SERVICE", "FAILED TO REMOVE CALL BACKS");
    }

    commTask.stopThread();
    //currentThread = null;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:35:46+00:00Added an answer on May 22, 2026 at 4:35 pm

    This is how I did it. Of course, you need to pass appropriate context.

       Handler h = new Handler(context.getMainLooper());
    
        h.post(new Runnable() {
            @Override
            public void run() {
                 Toast.makeText(context,message,Toast.LENGTH_LONG).show();
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow up question from my question thread exiting error in android
Sending out SMS messages from my Android phone in Java, how can I do
My question is: I am using spinner on my android app. However, I can't
Can Android OS that is installed on actual devices be emulated in VMWare or
On a touch device like iPhone/iPad/Android it can be difficult to hit a small
I'm looking for a java library that works on the android that can download
Android comes with lots of system resources ( android.R ) that can be used
I've got another question regarding to basic Android programming: How can I access the
Friends, I like to know using which version of Android SDK we can develop
How can we use Web services in Android Applications?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.