SlidePub
Home
Categories
Login
Register
Home
Technology
Android Threading
Android Threading
pohjus
2,443 views
17 slides
Apr 01, 2014
Slide
1
of 17
Previous
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About This Presentation
No description available for this slideshow.
Size:
504.46 KB
Language:
en
Added:
Apr 01, 2014
Slides:
17 pages
Slide Content
Slide 1
Android'Threading'
Jussi'Pohjolainen'
Tampere'University'of'Applied'Sciencs'
Slide 2
UI'Thread'
• When'Android'app'is'launched'one'thread'is'
created.'This'thread'is'called'Main'Thread'or'
UI#Thread#
• UI'Thread'is'responsible'for'dispatching+events+
to+widgets+
• Avoid#doing#0me#consuming#tasks#in#UI#
Thread#since#it#leads#to#app#that#does#not#
respond#quickly#
Slide 3
TesAng'UI'Responsivess'
public class ThreadExample extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Do Time Consuming task!");
setContentView(button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
for(int i=0; i<10; i++) {
System.out.println(i);
Thread.sleep(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Slide 4
Result'
Slide 5
TesAng'Separate'Thread'
public class ThreadExample extends Activity implements OnClickListener, Runnable {
...
@Override
public void onClick(View v) {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
try {
for(int i=0; i<10; i++) {
System.out.println(i);
Thread.sleep(10000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Slide 7
How'about'influencing'the'UI?'
...
@Override
public void run() {
try {
for(int i=0; i<10; i++) {
button.setText("Iteration: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
...
Slide 8
Problem'
Slide 9
Why?'
• Android'UI'Toolkit'is#not#thread#safe#
• If'you'want'to'manipulate'UI,'you#must#do#it#
inside#the#UI#thread#
• How'do'you'do'it'then?'You'can'use'
– Activity.runOnUiThread(Runnable)
– View.post(Runnable)
– View.postDelayed(Runnable, long)
– …
Slide 10
Activity.runOnUiThread(Runnable)
• The'given'acAon'(Runnable)'is'executed'
immediately'if'current'thread'is'UI'thread'
• If'current'thread'is'NOT'UI'thread,'the'acAon'
(Runnable)'is'posted'to'event'queue'of'the'UI'
Thread'
Slide 11
Example'of'RunOnUiThread'
public class ThreadExample extends Activity implements OnClickListener, Runnable {
...
@Override
public void onClick(View v) {
Thread t = new Thread(this);
t.start();
}
public void run() {
// lengthy operation
try {
Thread.sleep(2000);
} catch (InterruptedException e) { }
runOnUiThread(new Update());
}
class Update implements Runnable {
// This action is posted to event queue
public void run() {
button.setText("Finished!");
}
}
}
Slide 12
View.post(Runnable)
View.postDelayed(Runnable, Long)
• These'methods'are'of'view'and'are'use'for'
updaAng'the'view'
• AcAon'(Runnable)'is'placed'on'Message'
Queue'
• Runnable'acAon'runs'on'UI'Thread'
• postDelayed'method'for'delayed'acAon'
Slide 13
Using'post'
private int iteration;
...
@Override
public void run() {
try {
for(int i=0; i<10; i++) {
iteration = i;
button.post(new InfluenceUIThread());
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class InfluenceUIThread implements Runnable {
public void run() {
button.setText("Iteration = " + iteration);
}
}
Slide 14
Using'Anonymous'Inner'Classes'
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
try {
for(int i=0; i<10; i++) {
iteration = i;
button.post(new Runnable() {
public void run() {
button.setText("Iteration = " + iteration);
}
});
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
This'can'be'really'
confusing...'
Slide 15
AsyncTask
• Goal:'take'care'thread'management'for'you'
• Use'it'by'subclassing'it:'class'MyTask'extends'
AsyncTask'
• Override'onPreExecute(),'onPostExecute()'
and'onProgressUpdate()
– Invokes'in'UI'Thread'
• Override'doInBackground()
– Invokes'in'worker'thread'
Slide 16
Example'(Google'SDK)'
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
new DownloadFilesTask().execute(url1, url2, url3);
Params,'Progress,'Result'
Slide 17
public class ThreadExample extends Activity implements OnClickListener {
private Button button;
...
class MyBackgroundTask extends AsyncTask<Integer, Integer, Integer> {
protected Integer doInBackground(Integer... ints) {
int i = ints[0];
try {
for(i=0; i<10; i++) {
System.out.println("doInBackground!");
publishProgress(new Integer(i));
Thread.sleep(1000);
}
} catch(Exception e) {
e.printStackTrace();
}
return i;
}
protected void onProgressUpdate(Integer iteration) {
button.setText("Iteration = " + iteration);
}
protected void onPostExecute(Integer result) {
button.setText("Finished with result of: " + result);
}
}
}
Tags
Categories
Technology
Download
Download Slideshow
Get the original presentation file
Quick Actions
Embed
Share
Save
Print
Full
Report
Statistics
Views
2,443
Slides
17
Favorites
4
Age
4264 days
Related Slideshows
11
8-top-ai-courses-for-customer-support-representatives-in-2025.pptx
JeroenErne2
48 views
10
7-essential-ai-courses-for-call-center-supervisors-in-2025.pptx
JeroenErne2
47 views
13
25-essential-ai-courses-for-user-support-specialists-in-2025.pptx
JeroenErne2
37 views
11
8-essential-ai-courses-for-insurance-customer-service-representatives-in-2025.pptx
JeroenErne2
34 views
21
Know for Certain
DaveSinNM
22 views
17
PPT OPD LES 3ertt4t4tqqqe23e3e3rq2qq232.pptx
novasedanayoga46
26 views
View More in This Category
Embed Slideshow
Dimensions
Width (px)
Height (px)
Start Page
Which slide to start from (1-17)
Options
Auto-play slides
Show controls
Embed Code
Copy Code
Share Slideshow
Share on Social Media
Share on Facebook
Share on Twitter
Share on LinkedIn
Share via Email
Or copy link
Copy
Report Content
Reason for reporting
*
Select a reason...
Inappropriate content
Copyright violation
Spam or misleading
Offensive or hateful
Privacy violation
Other
Slide number
Leave blank if it applies to the entire slideshow
Additional details
*
Help us understand the problem better