Skip to content

Commit 4e51b3a

Browse files
author
sundyzlh
committed
1 parent 46eaec5 commit 4e51b3a

File tree

9 files changed

+159
-3
lines changed

9 files changed

+159
-3
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#Thu May 26 20:50:54 KST 2011
1+
#Sat Jun 11 16:34:25 KST 2011
22
eclipse.preferences.version=1
33
encoding//src/sundy/android/demo/LauncherActivity.java=UTF-8
44
encoding//src/sundy/android/demo/SundyAndroidDemoApplication.java=UTF-8
55
encoding//src/sundy/android/demo/configration/CommonConstants.java=UTF-8
6+
encoding//src/sundy/android/demo/processthread/AsyncTaskTestActivity.java=UTF-8
67
encoding//src/sundy/android/demo/processthread/ProcessLifecycleActivity.java=UTF-8
78
encoding//src/sundy/android/demo/processthread/ThreadConceptActivity.java=UTF-8

sundyandroid/Demos/SundyAndroidDemo/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
</intent-filter>
6262
</activity>
6363

64+
<activity android:name=".processthread.AsyncTaskTestActivity" android:label="@string/process_asynctask">
65+
<intent-filter>
66+
<action android:name="android.intent.action.MAIN" />
67+
<category android:name="android.intent.category.SAMPLE_CODE" />
68+
</intent-filter>
69+
</activity>
70+
6471
<service android:name=".service.LauncherService">
6572
</service>
6673
</application>

sundyandroid/Demos/SundyAndroidDemo/default.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
# project structure.
99

1010
# Project target.
11-
target=android-8
11+
target=android-4
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="fill_parent"
5+
android:orientation="vertical"
6+
android:layout_height="fill_parent">
7+
<EditText android:text="" android:id="@+id/editTextState2" android:layout_height="wrap_content" android:layout_width="fill_parent"></EditText>
8+
<ProgressBar android:id="@+id/progress_bar2"
9+
android:layout_width="200dip" android:layout_height="10dip"
10+
android:layout_gravity="center"
11+
android:max="100" style="?android:attr/progressBarStyleHorizontal"
12+
android:progress="0"/>
13+
<TextView android:id="@+id/text_view2" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
14+
<Button android:id="@+id/update_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Update"/>
15+
</LinearLayout>

sundyandroid/Demos/SundyAndroidDemo/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
<string name="process_activitymaintest">应用框架/Activity/Activity介绍及生命周期</string>
99
<string name="process_threadconcept">应用框架/进程和线程/线程概述</string>
1010
<string name="process_handlerconcept">应用框架/进程和线程/Handler-不可缺少的异步</string>
11+
<string name="process_asynctask">应用框架/进程和线程/AsyncTask-异步任务</string>
1112
</resources>

sundyandroid/Demos/SundyAndroidDemo/src/sundy/android/demo/LauncherActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import java.util.Map;
1010

1111
import sundy.android.demo.configration.CommonConstants;
12+
1213
import android.app.ListActivity;
1314
import android.content.Intent;
1415
import android.content.pm.PackageManager;
1516
import android.content.pm.ResolveInfo;
1617
import android.os.Bundle;
18+
import android.util.Log;
1719
import android.view.View;
1820
import android.widget.ListView;
1921
import android.widget.SimpleAdapter;

sundyandroid/Demos/SundyAndroidDemo/src/sundy/android/demo/SundyAndroidDemoApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44
package sundy.android.demo;
55

6-
import sundy.android.demo.configration.CommonConstants;
76
import android.app.Application;
87
import android.util.Log;
8+
import sundy.android.demo.configration.*;
99

1010
/**
1111
* @author sundy
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package sundy.android.demo.processthread;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.InputStream;
5+
6+
import org.apache.http.HttpEntity;
7+
import org.apache.http.HttpResponse;
8+
import org.apache.http.client.HttpClient;
9+
import org.apache.http.client.methods.HttpGet;
10+
import org.apache.http.impl.client.DefaultHttpClient;
11+
12+
import sundy.android.demo.R;
13+
14+
import android.app.Activity;
15+
import android.os.AsyncTask;
16+
import android.os.Bundle;
17+
import android.view.View;
18+
import android.view.View.OnClickListener;
19+
import android.widget.Button;
20+
import android.widget.EditText;
21+
import android.widget.ProgressBar;
22+
import android.widget.TextView;
23+
24+
public class AsyncTaskTestActivity extends Activity {
25+
26+
EditText asEditText = null ;
27+
TextView asTextView = null ;
28+
ProgressBar asProgressBar = null ;
29+
Button asButton = null ;
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
// TODO Auto-generated method stub
34+
super.onCreate(savedInstanceState);
35+
this.setContentView(R.layout.layout_asynctask) ;
36+
asEditText = (EditText)this.findViewById(R.id.editTextState2) ;
37+
asTextView = (TextView)this.findViewById(R.id.text_view2) ;
38+
asProgressBar = (ProgressBar)this.findViewById(R.id.progress_bar2) ;
39+
asButton = (Button)this.findViewById(R.id.update_button2) ;
40+
41+
asButton.setOnClickListener(new OnClickListener()
42+
{
43+
44+
@Override
45+
public void onClick(View v) {
46+
// TODO Auto-generated method stub
47+
if(asEditText.getText() != null && !asEditText.getText().equals(""))
48+
{
49+
new WebGetAsyncTask().execute(asEditText.getText().toString()) ;
50+
}
51+
52+
}
53+
54+
}) ;
55+
}
56+
57+
class WebGetAsyncTask extends AsyncTask<String,Integer,String>
58+
{
59+
60+
@Override
61+
protected void onPostExecute(String result) {
62+
// TODO Auto-generated method stub
63+
super.onPostExecute(result);
64+
asTextView.setText(result) ;
65+
}
66+
67+
68+
69+
@Override
70+
protected void onProgressUpdate(Integer... values) {
71+
// TODO Auto-generated method stub
72+
super.onProgressUpdate(values);
73+
//asProgressBar.setProgress(values)
74+
}
75+
76+
77+
78+
@Override
79+
protected String doInBackground(String... params) {
80+
try {
81+
HttpClient client = new DefaultHttpClient();
82+
// params[0] is connectivity url
83+
HttpGet get = new HttpGet(params[0]);
84+
HttpResponse response = client.execute(get);
85+
HttpEntity entity = response.getEntity();
86+
long length = entity.getContentLength();
87+
InputStream is = entity.getContent();
88+
String s = null;
89+
if (is != null) {
90+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
91+
byte[] buf = new byte[128];
92+
int ch = -1;
93+
int count = 0;
94+
while ((ch = is.read(buf)) != -1) {
95+
baos.write(buf, 0, ch);
96+
count += ch;
97+
if (length > 0) {
98+
// call publishProgress()update progress
99+
publishProgress((int) ((count / (float) length) * 100));
100+
}
101+
// sleep 100
102+
Thread.sleep(100);
103+
}
104+
s = new String(baos.toByteArray()); }
105+
return s;
106+
} catch (Exception e) {
107+
e.printStackTrace();
108+
}
109+
return null;
110+
}
111+
112+
}
113+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sundy.android.demo.processthread;
2+
3+
import sundy.android.demo.configration.CommonConstants;
4+
import android.os.Handler;
5+
import android.os.Message;
6+
import android.util.Log;
7+
8+
public class MyThreadHandler extends Handler {
9+
10+
@Override
11+
public void handleMessage(Message msg) {
12+
// TODO Auto-generated method stub
13+
super.handleMessage(msg);
14+
Log.i(CommonConstants.LOGCAT_TAG_NAME, "Get the message by Handler Thread") ;
15+
}
16+
17+
}

0 commit comments

Comments
 (0)