I’ve designed a code that sends file from android mobile to PC. The code runs absolutely fine , however there is a problem that activity screen displays after entire code has executed.
I wanted to add button to pause or stop transfer. However, even the button is displayed after entire transfer is done.
I’ve also tried using sleep after setContentView() but that doesn’t solve my problem.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
status=(TextView)findViewById(R.id.status);
int x;
DatagramSocket s = new DatagramSocket(2436);
FileInputStream fis = new FileInputStream("//sdcard//output.3gp");
while(true)
{
b2=new byte[100];
x=fis.read(b2);
if(x<=0)
{
b2=new byte[10];
p3=new DatagramPacket(b2,b2.length,client_ip,client_port);
s.send(p3);
break;
}
else
{
p3=new DatagramPacket(b2,b2.length,client_ip,client_port);
s.send(p3);
//status.append(b2[0]+" "+p3.getLength());
}
}
fis.close();
Don’t do long tasks from main thread. That will block UI. Move your long tasks to another threads.
Use an AsyncTask. Look at this: http://developer.android.com/resources/articles/painless-threading.html