Android: force a thread to close and display an alert dialog - android

I have a thread inside which I catch an exception. What I want is that when this happens, the thread closes/quits/dies or whatever I should say, and an alert dialog is displayed (no like toasts!).
Here's my code:
t1 = new Thread(new Runnable()
{
public void run()
{
Looper.prepare();
StringBuffer stringBuffer = new StringBuffer("");
BufferedReader bufferedReader = null;
URI uri = null;
try
{
requestAndMakeSheet(stringBuffer, bufferedReader, uri);
}
catch (Exception e)
{
//Toast.makeText(getBaseContext(), "Web Request Error", Toast.LENGTH_LONG).show();
//Log.e("Web Request Error", e.getMessage());
t1.interrupt();
AlertDialog.Builder parsingErrorBox = new AlertDialog.Builder(ReservationInfo.this);
parsingErrorBox.setTitle("Login error");
parsingErrorBox.setMessage("You may have to check your credentials and then try again.");
parsingErrorBox.show();
}
finally
{
if (bufferedReader!=null)
{
try
{
bufferedReader.close();
}
catch (IOException ioe)
{
Log.e("Web Request Error", ioe.getMessage());
}
}
}
}
});
t1.start();
try
{
t1.join();
mWebview.loadUrl("file:///"+Environment.getExternalStorageDirectory()+"/MySheet.html");
setContentView(mWebview);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//End of le thread
}
This code crashes, I think it has something to do with t1.interrupt (tried stop instead, but didn't work either).
How can I fix this code?
Thank you in advance.

Why do you have the t1.interrupt() anyway? Seems like your thread will be terminated regardless so what is the reason for putting it there?

Related

FileStreamInput read() works just once

I have a FileInputStream that reads data from arduino. On my java code reading function in an infinite while loop. read function works just once in the beginning and stop doing its work and jumps to catch block continuously.
Why does it do that ONCE? It should read data continuously. Any suggestions?
while(true)
{
try
{ byte[] inputmessage =new byte[1];
ret=mInputStream.read(inputmessage);
SystemClock.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
SystemClock.sleep(2000);
}
finally{
try {
if(mInputStream!=null){
mInputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
veri = Arrays.toString(inputmessage);
veri = veri.replace("[","");
veri = veri.replace("]","");
publishProgress(Integer.parseInt(veri));
SystemClock.sleep(2000);
}
}
An inputstream will go through data only once, you can check with "markSupported()" if you can set a mark in your stream, if you can, you can set it and use "reset" when you want to restart from scratch.

Thread in Service causes ANR

I'm trying to respond to multicast DatagramPackets on my phone. This is the part of the code that keeps causing the ANR:
private void multicastLoop() {
String res = Build.FINGERPRINT + "\n";
final InetAddress group;
final MulticastSocket socket;
final DatagramPacket response;
try {
group = InetAddress.getByName("239.255.255.127");
socket = new MulticastSocket(port);
socket.setLoopbackMode(true);
socket.setSoTimeout(1000);
socket.joinGroup(group);
response = new DatagramPacket(res.getBytes(), res.length(), group, port);
} catch (IOException e) {
e.printStackTrace();
return;
}
Thread t = new Thread(new Runnable() {
#Override
public void run() {
while(isRunning) {
try {
byte[] data = new byte[1024];
DatagramPacket dm = new DatagramPacket(data, data.length);
socket.receive(dm);
if (Arrays.equals(dm.getData(), "someone there".getBytes())) {
socket.send(response);
}
} catch (SocketTimeoutException e) {
continue;
} catch (IOException e) {
e.printStackTrace();
}
}
try {
socket.leaveGroup(group);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.run();
}
The method multicastLoop is called in the onCreate of the Service, after setting isRunning = true; Why does this Thread cause an ANR error? The TCP-Server-Thread is running without problems (while (isRunning) {...})
You need to call t.start(); instead of t.run();
t.run() will just execute the Runnable on the current thread (the UI) which causes the ANR.

Async task, BufferedReader

I have a BufferedReader, when I try to read it, it just hangs and doesn't do anything, am I doing this right? I am using this in an AsyncTask.
Edit: I have a tablet connected to the Wi-Fi, this connects to my computer which is broadcasting on 172.20.104.203 on port 5334, I can see when the thread starts, but nothing after that.
Here my code:
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
final String msg;
msg = (line);
Log.d("DeviceActivity", msg);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
EDIT
I have all the right permissions or anything, I was doing this outside a AsyncTask and it worked perfectly, moved it because I didn't want it in the main thread.
-Edit , here is the full code.
public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
Socket nsocket; // Network Socket
InputStream nis; // Network Input Stream
OutputStream nos; // Network Output Stream
private Handler handler = new Handler();
Boolean connected = false;
public static final int PORT = 5334;
public String SERVERIP = "172.20.104.203";
Socket socket;
#Override
protected void onPreExecute() {
Log.i("AsyncTask", "onPreExecute");
InetAddress serverAddr;
try {
serverAddr = InetAddress.getByName(SERVERIP);
socket = new Socket(serverAddr, PORT);
connected = true;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ClientAcivtity: Exception", String.valueOf(e));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ClientAcivtity: Exception", String.valueOf(e));
}
}
#Override
protected Boolean doInBackground(Void... params) { // This runs on a
// different thread
boolean result = false;
try {
Log.d("ClientActivity", "C: Connecting...");
if (socket != null) {
int cont = 1;
while (cont == 1) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
// where you issue the commands
out.println("getPos");
Log.d("ClientActivity", "C: Sent " + "getPos");
} catch (Exception e) {
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
final String msg;
msg = (line);
Log.d("DeviceActivity", msg);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ClientAcivtity: Exception",
String.valueOf(e));
}
cont--;
}
Log.d("ClientActivity", "C: Closed.");
}
} catch (Exception e) {
Log.e("ClientAcivtity: Exception", String.valueOf(e));
}
return result;
}
#Override
protected void onProgressUpdate(byte[]... values) {
if (values.length > 0) {
Log.i("AsyncTask", "onProgressUpdate: " + values[0].length
+ " bytes received.");
}
}
#Override
protected void onCancelled() {
Log.i("AsyncTask", "Cancelled.");
}
#Override
protected void onPostExecute(Boolean result) {
if (socket != null) {
if (connected) {
if (result) {
Log.i("AsyncTask",
"onPostExecute: Completed with an Error.");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.i("AsyncTask", "onPostExecute: Completed.");
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
My guess is that when you write out the command "getPos" the underlying BufferedWriter is not actually sending the data out on the line (you should verify this with tcpdump/wireshark). If this is the case, the server doesn't responsed to the readLine(), since it never got a command. To verify this claim, add out.flush(); after out.println("getPos");
Really, tcpdump will probably give you a better answer then anyone on the forums.
Also see http://developer.android.com/reference/java/io/BufferedWriter.html
Try doing it like this:
final BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
StringBuffer buf = new StringBuffer();
int i;
while((i = in.read()) != -1){
buf.append((char) i);
}
String data = buf.toString();
Reading from sockets is a quite difficult issue depending where the socket is actually connected to and how the other side responds.
If the other side is extremely fast than it can provide the socket with enough data so that the read routines actually work fine. However if there is a delay in the other side of any kind (just needs to be slower than your read routine incl the small default timeout) then your read fails even though there might be data on the other side - just arriving a little too slow at the socket.
Depending on your needs you may wrap your own minimum and maximum timer around the read routine.
Please provide more information and we can better understand the issue.
In many cases it is necessary to have a minimum timeout large enough for the other side to push data to the socket - but you might also need a maximum time for how long you actually want to wait for data to arrive.
UPDATE:
first the runnable to start the monitoring thread. You may use monitoringCanRun in your loop to interrupt the thread if required. And monitoringThreadIsAlive can be used to know if the thread is still running.
monitoringCanRun = true;
new Thread(new Runnable() {
public void run() {
monitoringThreadIsAlive = true;
performMonitoring();
monitoringThreadIsAlive = false;
}
}).start();
}
and performMonitoring looks like:
public void performMonitoring() {
while (monitoringCanRun) {
... do your read in the while loop
...you might like to insert some delay before trying again...
try { //we delay every partial read so we are not too fast for the other side
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Runtime.exec() in Android hangs

When I try to exec an external script in this way:
try {
process = Runtime.getRuntime().exec(
new String[] { "/system/bin/sh", "./myscript.sh" },
null,
"/data/mydir",
);
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
} catch (SecurityException e) {
Log.e(TAG, e.getMessage(), e);
}
Sometimes the script gets executed, but most often my app hangs a couple of seconds until Android says my app is unresponsive and it needs to kill it.
My question is, what may be happening. The script is running sometimes, and there is no exception being thrown, it just hangs. I'm at a loss as to what's happening. I'm using Froyo (2.2.1 I think).
Thanks!
According to the documentation you should read the err and out stream of the process.
http://developer.android.com/reference/java/lang/Process.html
I think something like the following will solve your problem.
class Reader extends Thread
{
InputStream is;
Reader(InputStream is){
this.is = is;
}
public void run()
{
try
{
InputStreamReader inStreamReader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(inStreamReader);
String line=null;
while ( (line = br.readLine()) != null){
// log here
}
} catch (IOException ex){
ex.printStackTrace();
}
}
}
Use the above class in your code like this
try {
process = Runtime.getRuntime().exec(
new String[] { "/system/bin/sh", "./myscript.sh" },
null,
"/data/mydir",
);
Reader err = new Reader(process.getErrorStream());
Reader output = new Reader(process.getInputStream());
err.start();
outout.start();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
} catch (SecurityException e) {
Log.e(TAG, e.getMessage(), e);
} finally {
process.destroy();
}

Android TCP client

I'm currently working on a tcp client in Android.
I want to connect my android device to a tcp server on my computer and receive the data once every 2 seconds. The problem is that I'm getting force close on my application because of the while loop that I've implemented in the tcp client.
I've tried writing in different ways the loop that will make the tcp client checking the server socket, but with no success. How can make a loop that will check the server socket without getting the force close?
Here's my code that I'm currently using:
public class Connection implements Runnable {
#Override
public void run() {
try {
sk=new Socket(server,port);
viewsurface.setText("connected");
flag = true;
} catch (UnknownHostException e) {
viewsurface.setText("failed 1 socket");
flag = false;
} catch (IOException e) {
viewsurface.setText("failed 2 socket");
flag = false;
}
while (flag == true){
try {
checkin = sk.getInputStream();
checkint = checkin.available();
if (checkint > 0){
try {
BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
received = in.readLine();
viewsurface.setText(received);
} catch (IOException e) {
viewsurface.setText("failed to receive");
}
}
Thread.sleep(2000);
} catch (IOException e) {
viewsurface.setText("checkin failed");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
You need to paste the exception that you are getting to cause the force close, before anyone can provide decent help.
But some suggestions that might solve the problem.
Most likely to be the problem, viewText.setText can only be called from the UI thread. There's quite a few ways to handle this. You can use AsyncTask or if you have an Activity reference you can use runOnUIThread and pass in a runnable that calls setText.
Move checkin = sk.getInputStream(); to before the loop. There's no reason to get the strem every cycle through the loop.
Do not create the BufferedReader every cycle through the loop. Move it before the loop
.sleep(2000) does not guarantee exactly 2 seconds.
I'm having some code formatting issues so I apologize.
private class DownloadFilesTask extends AsyncTask<Void, String, Void> {
protected Long doInBackground(Void... nothing) {
try {
sk=new Socket(server,port);
publishProgress("connected");
flag = true;
} catch (UnknownHostException e) {
publishProgress("failed 1 socket");
flag = false;
} catch (IOException e) {
publishProgress("failed 2 socket");
flag = false;
}
while (flag == true){
try {
checkin = sk.getInputStream();
checkint = checkin.available();
if (checkint > 0){
try {
BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
received = in.readLine();
publishProgress(received);
} catch (IOException e) {
publishProgress("failed to receive");
}
}
Thread.sleep(2000);
} catch (IOException e) {
updateProgress(
} catch (InterruptedException e) {
e.printStackTrace();
}
return;
}
protected void onProgressUpdate(String... progress) {
viewsurface.setText(progress[0]);
}
protected void onPostExecute(Void result) {
//nothing
}
}

Categories

Resources