Android:如何从远程服务中运行的线程显示 toast?

发布于 2024-11-10 02:34:54 字数 4538 浏览 0 评论 0原文

Android:如何从远程服务中运行的线程显示 toast?每当我从服务中的不同线程运行时,应用程序就会崩溃......

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;
}

}

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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绾颜 2024-11-17 02:34:54

我就是这样做的。当然,您需要传递适当的上下文。

   Handler h = new Handler(context.getMainLooper());

    h.post(new Runnable() {
        @Override
        public void run() {
             Toast.makeText(context,message,Toast.LENGTH_LONG).show();
        }
    });

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();
        }
    });
请别遗忘我 2024-11-17 02:34:54

如果有人不明白 @Alex Gitelman 答案中的上下文。在您编写此处理程序代码的上下文中写下您的类名,如下所示

Handler h = new Handler(MyClassName.this.getMainLooper());

h.post(new Runnable() {
    @Override
    public void run() {
         Toast.makeText(MyClassName.this,"show toast message",Toast.LENGTH_LONG).show();
    }
});

if someone don't understand what context is in @Alex Gitelman answer. write your class name in place of context in which you are writing this handler code like this

Handler h = new Handler(MyClassName.this.getMainLooper());

h.post(new Runnable() {
    @Override
    public void run() {
         Toast.makeText(MyClassName.this,"show toast message",Toast.LENGTH_LONG).show();
    }
});
魔法少女 2024-11-17 02:34:54

我不确定,但这看起来又像是尝试在非 GUI 线程上调用 GUI 方法。

I am not sure but this looks again like trying to invoke GUI methods on non-GUI threads.

余生一个溪 2024-11-17 02:34:54

另一种尝试方法是使用 AsyncTask这篇 Android 文章是一篇关于线程的好文章。

Another way to try would be to use an AsyncTask. This Android article is a good one about threading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文