0% found this document useful (0 votes)
232 views

Threading in Android

Threading in Android allows an application to have multiple threads of execution running concurrently. There are two categories of threads in Android: 1) Threads attached to an activity/fragment that are terminated when the activity/fragment is destroyed, and 2) threads not attached to an activity/fragment like services. The example demonstrates using threads by creating a thread on a button click that updates two text views after 5 seconds. Loaders were introduced in Android 3.0 to asynchronously load data in an activity/fragment and automatically restart on configuration changes.

Uploaded by

Muzamil Yousaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views

Threading in Android

Threading in Android allows an application to have multiple threads of execution running concurrently. There are two categories of threads in Android: 1) Threads attached to an activity/fragment that are terminated when the activity/fragment is destroyed, and 2) threads not attached to an activity/fragment like services. The example demonstrates using threads by creating a thread on a button click that updates two text views after 5 seconds. Loaders were introduced in Android 3.0 to asynchronously load data in an activity/fragment and automatically restart on configuration changes.

Uploaded by

Muzamil Yousaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

THREADING IN ANDROID

WHAT IS THREAD

• A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have
multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are
executed in preference to threads with lower priority.
• In Android, you can categorize all threading components into two basic categories:
• Threads that are attached to an activity/fragment: These threads are tied to the lifecycle of the activity/fragment and
are terminated as soon as the activity/fragment is destroyed.
• LOADERS
• Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment. monitor the
source of their data and deliver new results when the content changes. They automatically reconnect to the
last loader's cursor when being recreated after a configuration change.

Loaders are the solution for the problem mentioned above. Loaders can automatically stop when the activity is
destroyed, and can also restart themselves after the activity is recreated.
T H R E A D I N G C O M P O N E N T S T H AT D O N ’ T AT TA C H TO A N A C T I V I T Y / F R A G M E N T

SERVICE
• Service is a component that is useful for performing long (or potentially long) operations without any UI.
• Service runs in the main thread of its hosting process; the service does not create its own thread and does
not run in a separate process unless you specify otherwise.
•  A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This
example demonstrate about How to use multiple threads in android.
EXAMPLE

• Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
• Step 2 − Add the following code to res/layout/activity_main.xml.
• <?xml version="1.0" encoding="utf-8"?>
• <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
• xmlns:tools="http://schemas.android.com/tools"
• android:layout_width="match_parent"
• android:layout_height="match_parent"
• android:orientation="vertical"
• android:gravity="center_horizontal"
• android:layout_marginTop="100dp"
• tools:context=".MainActivity">
<EditText android:textColor="#FFF"
android:id="@+id/edit_query" android:layout_height="wrap_content"
android:layout_width="match_parent" android:text="Button" />
android:layout_height="wrap_content" <TextView
android:hint="Enter string" /> android:id="@+id/text"
<Button android:layout_width="wrap_content"
android:id="@+id/click" android:layout_height="wrap_content" />
android:layout_marginTop="50dp" <TextView
android:id="@+id/text1"
style="@style/Base.TextAppearance.AppCompat.Widge
android:layout_width="wrap_content"
t.Button.Borderless.Colored"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
</LinearLayout>
android:background="#c1c1c1"
• In the above code, we have taken edittext and textview. When user enter some text into edittext, it going to wait till 5000ms and update
both textview’s with thread name.
• Step 3 − Add the following code to src/MainActivity.java
package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


EditText edit_query;
TextView textView;
TextView text1;
boolean twice = false;
Thread t = null;
@Override }
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); private void runthread1() {
setContentView(R.layout.activity_main); runOnUiThread(new Runnable() {
edit_query = findViewById(R.id.edit_query); @Override
textView = findViewById(R.id.text); public void run() {
text1 = findViewById(R.id.text1); try {
findViewById(R.id.click).setOnClickListener(new Thread.sleep(5000);
View.OnClickListener() {
} catch (InterruptedException e) {
@Override
e.printStackTrace();
public void onClick(View v) {
}
runthread();
text1.setText("tutorialspoint.com");
runthread1();
}
}
});
}); textView.setText(t.getName());
} twice = false;
}
private void runthread() { });
twice = true; }
if (twice) { });
final String s1 = t.start();
edit_query.getText().toString();
t.setName(s1);
t = new Thread(new Runnable() {
t.setPriority(Thread.MAX_PRIORITY);
@Override
}
public void run() {
}
runOnUiThread(new Runnable() {
}
@Override
public void run() {
• Let's try to run your application. I assume you have connected your actual Android
Mobile device with your computer. To run the app from android studio, open one of your
project's activity files and click Run   icon from the toolbar. Select your mobile device as
an option and then check your mobile device which will display your default screen

You might also like