gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:01 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于Android:如何从远程服务中运行的线程中显示 toast ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6134013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com