A simple example of Android in ProgressDialog
Advertisements
The establishment of android and engineering work is omitted, Google click on it.
Here to introduce the main Activity
ProgressBarDemo.java
package com.lveyo.android.demo.progressbar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ProgressBarDemo extends Activity {
private TextView statusTextView;
private Button beginBtn;
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
statusTextView = (TextView)findViewById(R.id.status);
beginBtn = (Button)findViewById(R.id.beginBtn);
setListener();
}
/**
* Used to update UI Handler
*/
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// Close ProgressDialog
progressDialog.dismiss();
// Update the UI
statusTextView.setText("Completed!");
}};
/**
* Click on the button event listener
*/
private void setListener(){
beginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show ProgressDialog
progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);
// Create a new thread
new Thread(){
@Override
public void run() {
// Need to spend time calculation method
Calculation.calculate(4);
// To send a message handler
handler.sendEmptyMessage(0);
}}.start();
}
});
}
}
Calculation.java
package com.lveyo.android.demo.progressbar;
/**
* Schematic method
* @author lveyo
*
*/
public class Calculation {
public static void calculate(int sleepSeconds){
try {
Thread.sleep(sleepSeconds * 1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/beginBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="begin"
/>
</LinearLayout>
In the android, usually we can not in a separate thread to update UI, but in the main thread, which is why we want to use Handler, and when the handler receive the message, it will put it into the queue waiting for the implementation of , generally speaking this will soon be executed.
Related Posts of A simple example of Android in ProgressDialog
-
NoClassDefFoundError: javax / servlet / Servlet
In the project in order to achieve a listener in web.xml set up a listener, did start in Tomcat actually occurred after java.lang.NoClassDefFoundError: javax / servlet / ServletContextListener this anomaly google and found the reasons for the lack of serv
-
jboss ejb3 Message Driven Bean
Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Message Dri
-
java read file
java read documents in two ways: java.io and java.lang.ClassLoader When using the java.io, when java.lang.ClassLoader use it? (Note: If prior to read xml file java read file clearly aware of these two methods have been like! Can take much less to understa
-
JDBC example of a long time do not have JDBC forgot
A back-up here to stay. The first: The second:
-
JAVA interview questions
JAVA interview questions 1, object-oriented features of what has 1. Abstract 2. Inheritance 3. Packaging 4. Polymorphisms 2, String data types are the most basic right? Basic data types include byte, int, char, long, float, double, boolean and short. java
-
Build flex + spring + blazeds + hibernate application
Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...
-
JAVA EE JSP_JNDI
dsfdsa http://lindows.javaeye.com/admin/blogs/213348 Tomcat 6 with the connection pool data source configuration http://www.blogjava.net/ec2008/archive/2008/07/19/216063.html project: test Driver path: D: \ workspace \ test \ WebRoot \ WEB-INF \ lib ...
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co
-
Spring2.0 + hibernate3.1 + log4j + mysql demo
applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com












