Continuously updating GUI in java Android - java

I am a new java programmer in Eclipse for Android platform.
I am working on an android app which will receive multicast data through wifi interface and display the same in a TextView. The data will be updated every 1 sec.
I have the code as below. But i am facing problem in updating the GUI.in this code the network receive and gui are done in same thread i.e. main thread that's why my application in hanging.
I had tried using AsynchTask but could not succeed because i dont know how exactly to use it.
package com.example.cdttiming;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Seconds;
Seconds =(EditText)findViewById(R.id.Seconds);
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
InetAddress ia = null;
byte[] buffer = new byte[65535];
MulticastSocket ms = null;
int port = 4321;
try
{
ia = InetAddress.getByName("226.1.1.1");
DatagramPacket dp = new DatagramPacket(buffer, buffer.length,ia,port);
ms = new MulticastSocket(port);
ms.setReuseAddress(true);
ms.joinGroup(ia);
while (true)
{
ms.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
Seconds.setText(s);
}
}
catch (UnknownHostException e)
{
Seconds.setText(e.getMessage());
}
catch (IOException e)
{
Seconds.setText(e.getMessage());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Can anyone tell me how to use above code so that I may receive the data every second and display that in the TextView?
Thank u all in advance

AsyncTask does not really for your use case. If the background task needs to run continuously, you'd better just start a new thread. In the thread use a handler to communicate values to the UI. In the callback method of the handler, you can update the gui.
More info on handlers:
http://mobileorchard.com/android-app-developmentthreading-part-1-handlers/
http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html
http://developer.android.com/reference/android/os/Handler.html
Or google for good examples to get this going.
Edit:
Ok, what you need to do is start a new thread and in the run method put the part that reads from the socket.
You class create the thread as in inner class, something like:
class SocketThread extends Thread {
private final Handler mHandler;
private final WifiManager mWifiManager;
SocketThread(Handler handler, WifiManager wifiManager) {
mHandler = handler;
}
#override public void run() {
// socket code goes here
// whenever you receive a part that should update the ui,
// do something like:
final String s = new String(dp.getData(),0,dp.getLength());
mHandler.post(new Runnable() {
#override
public void run() {
update(s);
}
});
}
}
Then in your onCreate() create a handler and an instance of this thread and start it.
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText Seconds;
Seconds =(EditText)findViewById(R.id.Seconds);
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Handler handler = new Handler();
SocketThread thread = new SocketThread(handler, wm);
thread.start();
...
Also define a method update in you activity. Here you will receive the text you want to set. This method will look like this:
public void update(String s) {
Seconds.setText(s);
}
I advice you to save the seconds variable (the edittext) as a global class variable. This way you can write to it from the update method without having to findById it first.
I edited your code a bit, it may have to do with the wifi code. I am not familiar with this part of the framework. But I can imagine it has something to do with the WifiManager and connecting to it. If this doesn't work, set a breakpoint and step through it, you'll see where it stops doing anything.
package com.example.timing;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText Seconds;
String s;
Button button;
byte[] buffer = new byte[65535];
InetAddress ia = null;
int port = 4321;
DatagramPacket dp = new DatagramPacket(buffer, buffer.length,ia,port);
MulticastSocket ms = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Seconds = (EditText) findViewById(R.id.et_time);
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Handler handler = new Handler();
SocketThread thread = new SocketThread(handler, wm);
thread.start();
}
class SocketThread extends Thread {
private final Handler mHandler;
SocketThread(Handler handler, WifiManager wifiManager) {
mHandler = handler;
}
#Override
public void run() {
// socket code goes here
try {
wm.setWifiEnabled(true);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
ia = InetAddress.getByName("226.1.1.1");
ms.setReuseAddress(true);
ms.joinGroup(ia);
while(true) {
ms.receive(dp);
s = new String(dp.getData(),0,dp.getLength());
update(s);
}
} catch (UnknownHostException e) {
update(e.getMessage());
} catch (IOException e) {
update(e.getMessage());
}
}
}
public void update(String s)
{
Seconds.setText(s);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

You should not use AsyncTask for longer lasting activities - use a simple Thread and a Handler instead. In the thread, you do the WI-FI stuff (basically your while(true) loop) and whenever you want to update the UI, you pass a message to the Handler and handle the message within the UI thread.
For further information, please read the famous Painless Threading post from the Android Developers Blog.

Have a look through Handlers and Services
Do you only need to collect the data while your app is visible? The way you approach this challenge is very dependant on the needs and lifecycle of the data and communication.
AsyncTask is normally used to do one off jobs on a separate thread and then notify main thread when done.
Possibly look into a service as it is intended for long running jobs.

Related

Background changing every second

I am trying to change background every second and this is my code:
package com.example.splasher;
import java.lang.reflect.Field;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class Views extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.view);
final ScrollTextView scrolltext=(ScrollTextView) findViewById(R.id.scrolltext);
if(MainActivity.bold.isChecked())
{
scrolltext.setTypeface(null, Typeface.BOLD);
};
if(MainActivity.italic.isChecked())
{
scrolltext.setTypeface(null, Typeface.ITALIC);
};
if((MainActivity.italic.isChecked())&&(MainActivity.bold.isChecked()))
{
scrolltext.setTypeface(null, Typeface.BOLD_ITALIC);
};
scrolltext.setTextSize(Integer.parseInt(MainActivity.tSize[MainActivity.Size.getSelectedItemPosition()]));
scrolltext.setText(MainActivity.text);
scrolltext.setTextColor(Integer.parseInt(MainActivity.colour[MainActivity.TextColour.getSelectedItemPosition()]));
scrolltext.startScroll();
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[MainActivity.BackgroundColour.getSelectedItemPosition()]));
Thread thread = new Thread()
{
public void run()
{
if(MainActivity.random.isChecked())
{
int delay = 0; // delay for 5 sec.
int period = 1000; // repeat every sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int n=1;
public void run() {if (n==9)n=1;
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[n]));
}
}, delay, period);
/*int n=1;
boolean constant=true;
while (constant==true){
if (n==10) n=1;
// int randInt = new Random().nextInt(2);
// scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[randInt]));
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[n]));
n=n+1;
try
{
Thread.sleep(2000); // 1 second
} catch (Exception e)
{
e.printStackTrace();
}
}*/
}
}
};
thread.start();
// TextView textview=(TextView) findViewById (R.id.textView1);
// textview.setText(MainActivity.text);
// textview.setTextColor(Integer.parseInt(MainActivity.colour[MainActivity.TextColour.getSelectedItemPosition()]));
// textview.setBackgroundColor(Integer.parseInt(MainActivity.colour[MainActivity.BackgroundColour.getSelectedItemPosition()]));
// textview.setTextSize(Integer.parseInt(MainActivity.tSize[MainActivity.Size.getSelectedItemPosition()]));
// textview.setSelected(true);
}
}
But it force closes. Logcat shows: FATAL EXCEPTION: Timer-0; android.view.ViewRootŲcalledfromwrongThreadException: Only original threat that created a view hierarchy can touch its view.
What is wrong with this code?
You can not operate on the UI elements (like call scrolltext.setBackgroundColor()) in a thread separate from the so-called UI thread (which is basically the same thread onCreate() runs in.
To solve your issue, use a Handler. Create a class field:
Handler handler = new Handler();
And in your thread put all UI operations inside a Handler call:
handler.post(new Runnable() {
#Override
public void run () {
// All UI operations, like scrolltext.setBackgroundColor()
}
});
Your problem is
scrolltext.setBackgroundColor(Integer.parseInt(MainActivity.colour[n]));
You are updating UI from worker Thread(it not runs on UI Thread) and this is not allowed. Only Main(UI) Thread can manipulate with UI.
So you need to change it to runOnUiThread() or Handler.
Examples:
runOnUiThread(new Runnable(){
public void run() {
// do your stuff
}
});
Handler example.
Just trying to contribute here, but I have zero experience in android, so be cautions ;p
The exception message seems to suggest that the view can only be altered on the main thread. As you are running code which alters the view on a new thread, it doesn't like it.
Are you aware of a main Event-Dispatcher thread used in Android? Similar to how SWINGs GUI is updated on the EDT. If so, look for a way to tell the main drawing thread to do the final updates.
In swing, it would look something like:
EventQueue.invokeLater(new Runnable(){
public void run(){
}
}
Hope this has some meaning.

How can I handle multiple clients connected to a server using sockets?

I have an Android app that needs to let multiple devices connect. One device should act as the group owner and issue instructions to all of the clients to do specific things. I suppose it's comparable to a wireless handheld game where one player is the host.
I have a couple of questions so I will try to keep them concise. Even an answer to just the first would help.
First, I've successfully paired a single server and single client phone using sockets. I did this using Android's Wi-Fi Direct technology (Described here). The tutorial is helpful but unfortunately not very thorough, particularly in describing one-to-many connections. Once a peer list is found, socket connections can be opened. I was able to connect two devices using a thread for the server (Using this example), like so:
public class ServerThread implements Runnable {
public void run() {
(Code...)
client = serverSocket.accept();
(...other code here.)
}
}
A client is created following a button press (I think, still trying to get my head around my modified code):
public class MusicClientThread implements Runnable {
Socket socket = new Socket(serverAddr, port);
while (connected) {
//(Other code here.)
while ((line = in.readLine()) != null) {
//While there are still messages coming
}
}
socket.close();
//(Other code here too.)
}
So I suppose my first question is: How would I allow more clients to be connected? My ServerThread refers to a single client variable above so I do not see how I would allow a varying number (my application is aiming for anything from 2 to 10 users) nor do I know the correct way to differentiate between all my different clients. My only guess is that I would use the unique IP address of each phone.
My 2nd question is, once I've established a connection with multiple clients/peers, how would I then send and receive instructions to them correctly? Currently my single server awaits an instruction, and upon receiving it, issues a response instruction. I need it so that the server can send instructions from the start, using button presses, and the result of these is visible on the client devices.
I hope I have made everything clear!
You have to start a new thread for each client, as the sockets need a thread for themselves to run (as it waits for input most of the time). This can for example be done (in this simplified manner):
public class ThreadHandler extends Thread {
private ArrayList<ServerThread> serverThreads = new ArrayList<ServerThread>();
public void run() {
while (!isInterrupted()) { //You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
(Code...)
ServerThread thread = new ServerThread(serverSocket.accept());
serverThreads.add(thread);
thread.start();
}
}
public void doSomethingOnAllThreads() {
for (ServerThread serverThread : serverThreads) {
serverThread.otherMethod();
}
}
}
Where the ServerThread looks something like this:
public class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
public void run() {
(...other code here.)
}
public void otherMethod() {
//Signal to the thread that it needs to do something (which should then be handled in the run method)
}
}
I have a Google TV Box. And i have two mobile phones.
On the GoogleTV Box i run a server.
The Server has one ServerSocket on port 6001.
Also the Server has two sockets for two clients
The first device connects to server and using socket number one
the second the other....
I can simultanous post diffrent messages from 2 devices to the google tv box socket and show them on TV.
I using the following solution:
FOR THE MOBILE CLIENT (2 devices)
create a new android project with a blank activity and copy this code into.
create a layout for the client containing a edittext and a button.
MAKE SURE TO SET INTERNET AND ACCESS NETWORK PERMISSIONS IN ANDROIDMANIFEST.XML!!!
And edit serverIpAddress in this file to your servers accessible IP.
package de.android.googletv.gameclient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
*
*
*
*/
public class FullscreenActivity extends Activity {
private Socket socket;
private String serverIpAddress = "192.168.22.253"; // GOOGLE TV IP
private static final int PLAYER_1_SERVERPORT = 6001;
private Button bt;
private TextView tv;
String DEVICE_NAME;
private class ConnectToServer extends AsyncTask {
#Override
protected Object doInBackground(Object... params) {
System.out.println("connecting to server...");
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, PLAYER_1_SERVERPORT);
} catch (UnknownHostException e1) {
System.out.println("ERROR REACHING SERVER! 1");
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("ERROR REACHING SERVER! 2");
e1.printStackTrace();
}
System.out.println("Done!");
return params;
}
protected void onPostExecute() {
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
DEVICE_NAME = android.os.Build.MODEL;
Button exit = (Button) findViewById(R.id.dummy_button);
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.exit(1);
}
});
new ConnectToServer().execute("");
tv = (TextView) findViewById(R.id.editText1);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
Random rnd = new Random();
EditText et = (EditText) findViewById(R.id.editText1);
String str = DEVICE_NAME + " ID" + rnd.nextInt() + " says: " + et.getText().toString();
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true
);
out.println(str);
Log.d("Client", "Client sent message");
}
catch (UnknownHostException e) {
tv.setText("UnknownHostException");
e.printStackTrace();
}
catch (IOException e) {
tv.setText("IOException");
e.printStackTrace();
}
catch (Exception e) {
tv.setText("Exception");
e.printStackTrace();
}
}
});
}
}
FOR THE SERVER (google tv box)
create a new android project with blank activity and copy this code into.
create a layout with only a textfield
MAKE SURE TO SET INTERNET AND ACCESS NETWORK PERMISSIONS IN ANDROIDMANIFEST.XML!!!
package de.android.googletv.gameserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
*
*
*
*/
public class FullscreenActivity extends Activity {
// server socket
ServerSocket ss_plr1 = null;
public static final int SERVERPORT_1 = 6001;
int nr_connections = 0;
// socket for player1
Socket s1;
// socket for player2
Socket s2;
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
String mClientMsg = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
Button exit = (Button) findViewById(R.id.dummy_button);
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.exit(1);
}
});
TextView tv = (TextView) findViewById(R.id.fullscreen_content);
tv.setText("Nothing from client yet");
myCommsThread = new Thread(new CommsThread());
myCommsThread.start();
}
#Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
ss_plr1.close();
}
catch (IOException e) {e.printStackTrace(); }
}
Handler myUpdateHandler = new Handler() {
public void handleMessage(Message msg) {
System.out.println("handleMessage("+msg+")");
switch (msg.what) {
case MSG_ID:
TextView tv = (TextView) findViewById(R.id.fullscreen_content);
tv.setText((String)msg.obj);
break;
default:
break;
}
super.handleMessage(msg);
}
};
class CommsThread implements Runnable {
public void run() {
System.out.println("creating new sockets...");
try {
ss_plr1 = new ServerSocket(SERVERPORT_1 );
if (s1 == null)
s1 = ss_plr1.accept();
if (s2 == null)
s2 = ss_plr1.accept();
}
catch (IOException e) {e.printStackTrace();}
new Thread(new ConnectionHandler(s1, myUpdateHandler)).start();
new Thread(new ConnectionHandler(s2, myUpdateHandler)).start();
}
}
}
... and required by the server is the connection handler for threaded messaging ...
create a additional class in server project called: "ConnectionHandler.java" and copy this code into. it handles the async connections.
package de.android.googletv.gameserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import android.os.Handler;
import android.os.Message;
public class ConnectionHandler implements Runnable {
Socket m_socket;
Handler m_updateHandler;
public ConnectionHandler(Socket socket, Handler updateHandler) {
m_socket = socket;
m_updateHandler = updateHandler;
}
#Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
String st = null;
st = input.readLine();
Message m = new Message();
m.what = 0x1337;
m.obj = st;
m_updateHandler.sendMessage(m);
}
catch (IOException e) { e.printStackTrace();}
}
}
}
This is not the nicest solution. Multiple "not nice" codings. System.exit(1) for example. And it is only has two devices support. But it works for more than one device, and im pretty sure you will modify it for you purposes. Its based on three web sources and additional attemps from myself to make it work. Its only my prototype....
I CANNOT LINK TO THEM :( ... to less reputation.
If you build and run everthing it should look like this:
https://plus.google.com/u/0/109268217221135029753/posts/3iz6SF1hiJa

Android Threads and Messaging

I'm working on an Android app that uses uses a background worker thread. I need to be able to send messages to the thread from the activity, but I can't quite figure it out.
I have one activity, and one thread to do work in the background. I want to start the thread and be able to send messages (arguments, objects, etc.) to it when needed. I've mastered sending messages from the thread to the activity (by passing the activity's handler to the thread, and using that to send messages), but whenever I attempt to send messages from activity to thread, the app crashes.
I've tried following a good 10-12 tutorials that I've found online, all of which seemed to have a different way of doing things, but I still haven't gotten this to work correctly. Could someone please point me in the right direction?
An example simple activity:
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savesInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void doWork() {
Worker worker = new Worker();
worker.start();
worker.handler.sendEmptyMessage(0);
}
}
An example simple thread:
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
public class Worker extends Thread {
public Handler handler;
public void run() {
Looper.prepare();
handler = new Handler() {
public void handleMessage(Message msg) {
Log.d("Worker.run()", "Got message saying " + msg.what);
}
};
Looper.loop();
}
You have to read how to use and that for is Looper first and don't forget to stop looper at the end Goodluck
public class Worker extends Thread {
public Handler handler;
#Override
public run() {
Looper.prepare();
//initialization will take a little time you if you want to send message check if handler != null
handler = new Handler() {
public void handleMessage(Message msg) {
Log.d("Worker.run()", "Got message saying " + msg.what);
}
};
Looper.loop();
}
public void sendMessage(Message m)
{
while(handler == null);
handler.sendMessages(m);
}

Thread remains in NEW state if started from AsyncTask by runOnUiThread

Here is an example application I wrote to reproduce the memory leak related issue i have met :
package a.b.mapleak;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MapLeak extends Activity
{
TextView callVoidText, getNewDataText, getDataStringText;
Button callVoidButton, getNewDataButton, getDataStringButton;
Handler callbackHandler;
MemoryLeak memleak;
private LoadAccountsTask mLoadAccountsTask = null;
private class LoadAccountsTask extends AsyncTask<Void, Void, Object[]> {
private int mCursor1;
private int mCursor2;
private Context context;
private UITask t = new UITask();
#Override
protected Object[] doInBackground(Void... params) {
runOnUiThread(t);
// Create the summaries cursor
mCursor1 = 1;
mCursor2 = 2;
return new Object[] { mCursor1, mCursor2};
};
private class UITask extends Thread {
#Override
public void run() {
int i = 2;
while (i>0) {
i--;
}
Thread.State state=t.getState();
Log.e(ALARM_SERVICE, "Thread state in run is " + state.toString());
}
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getDataStringText = (TextView) findViewById(R.id.getDataString_text);
getDataStringButton = (Button) findViewById(R.id.getDataString_button);
getDataStringButton.setOnClickListener(new Button.OnClickListener() {
long i = 0;
public void onClick(View v) {
if (mLoadAccountsTask != null) mLoadAccountsTask.cancel(true);
mLoadAccountsTask = (LoadAccountsTask) new LoadAccountsTask(this).execute();
}
});
}
}
I found that the thread started in runOnUiThread has never been exited. For some unknown to me reason the task was running, but the output from
Log.e(ALARM_SERVICE, "Thread state in run is " + state.toString());
every time was "Thread state in run is NEW" e.g. ''The thread has been created, but has never been started.''. So the thread was running continuously preventing "LoadAccountsTask" class from being garbage collected. Thus, in this example, every time the button is pressed - the new "LoadAccountsTask" appeared in memory.
But, if to replace
private class UITask extends Thread
with
private class UITask implements Run
there was no memory leak, the thread exited successfully. Of course state=t.getState(); will not work in this case and should be commented.
Do anybody have an explanation why it is so?
Focusing on your immediate concern, runOnUiThread() takes a Runnable. While Thread does implement Runnable, runOnUiThread() will not treat it as a Thread. Please just pass a Runnable to runOnUiThread().
Second, never call runOnUiThread() from doInBackground(). Just perform the work from that Runnable in onPreExecute() or onPostExecute(), which are executed on the main application thread.
Third, if you ever do write a real Thread, don't busy-loop in it.

Thread in Android

I have an example like this:
package android.uiexample;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class BasicViewsExampleActivity extends Activity
{
private static int progress = 0;
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_views);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
//---do some work in background thread---
new Thread(new Runnable()
{
public void run()
{
//---do some work here---
while (progressStatus < 10)
{
progressStatus = doSomeWork();
}
//---hides the progress bar---
handler.post(new Runnable()
{
public void run()
{
progressBar.setVisibility(View.GONE);
}
});
}
//---do some long lasting work here---
private int doSomeWork()
{
try {
//---simulate doing some work---
Thread.sleep(500);
} catch (InterruptedException e)
{
e.printStackTrace();
}
return ++progress;
}
}).start();
}
}
In this example, it use Handler to post a Runable to exc progressBar.setVisibility(View.GONE);. I don't know why i can't call progressBar.setVisibility(View.GONE); directly:
//---do some work here---
while (progressStatus < 10)
{
progressStatus = doSomeWork();
}
//---hides the progress bar---
progressBar.setVisibility(View.GONE);
Anybody can tell me why i can't?
The android UI framework (like pretty much every UI framework before it) only allows you to update the UI state from the main thread. You may want to look at AsyncTask which include the plumbing needed to route progress updates onto the main thread.
Take a look at the Handler class. It provides a simple way to enqueue Runnable callbacks to run on the UI event thread.
changing the progressBar visibility is a UI operation. All UI operations must be done on the UI thread.

Categories