0% found this document useful (0 votes)
25 views2 pages

Practical 26

The document shows code for an Android application that uses an AsyncTask to insert data into a SQLite database. The AsyncTask takes a string parameter representing the sleep time in seconds. It displays a progress dialog while sleeping, then dismisses the dialog and displays the result once completed.

Uploaded by

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

Practical 26

The document shows code for an Android application that uses an AsyncTask to insert data into a SQLite database. The AsyncTask takes a string parameter representing the sleep time in seconds. It displays a progress dialog while sleeping, then dismisses the dialog and displays the result once completed.

Uploaded by

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

Practical 26

Q1) WAP to insert data in SQL lite database using AsyncTask


XML FILE:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Sleep time in seconds: "
android:textSize="10pt"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="36dp" />

<EditText
android:id="@+id/in_time"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignTop="@id/tv_time"
android:layout_marginLeft="36dp"
android:layout_marginTop="0dp"
android:layout_toRightOf="@+id/tv_time"
android:background="@android:drawable/editbox_background"
android:inputType="number"
tools:layout_editor_absoluteX="245dp"
tools:layout_editor_absoluteY="31dp" />

<Button
android:id="@+id/btn_run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/in_time"
android:layout_marginTop="17dp"
android:text="Run Async task"
tools:layout_editor_absoluteX="150dp"
tools:layout_editor_absoluteY="105dp" />

<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="7pt"
android:layout_below="@+id/btn_run"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

JAVA FILE:-
package com.example.practical26;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


private Button button;
private EditText time;
private TextView finalResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time=(EditText) findViewById(R.id.in_time);
button=(Button) findViewById(R.id.btn_run);
finalResult=(TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskRunner runner = new AsyncTaskRunner();
String sleepTime= time.getText().toString();
runner.execute(sleepTime);
}
});
}
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String...params) {
publishProgress("Sleeping...");
try {
int time= Integer.parseInt(params[0])*1000;
Thread.sleep(time);
resp="Slept for " + params[0]+ "seconds";
} catch (InterruptedException e){
e.printStackTrace();
resp=e.getMessage();
}catch (Exception e){
e.printStackTrace();
resp=e.getMessage();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
finalResult.setText(result);
}
@Override
protected void onPreExecute() {
progressDialog=ProgressDialog.show(MainActivity.this,"progressDialog","wait for"+time.getText().toString()+
"seconds");
}
@Override
protected void onProgressUpdate(String... text)
{
finalResult.setText(text[0]);
}
}
}

You might also like