0% found this document useful (0 votes)
2K views3 pages

Exercise Q.1) Write A Program To Insert Data in Sqlite Database Using Asynctask. Program Code: Activity - Main - XML

The document describes how to insert data into an SQLite database using an AsyncTask in Android. It includes XML layout code for a basic UI with a button and text view. It also includes Java code for a MainActivity class that uses an AsyncTask with doInBackground, onPreExecute, onProgressUpdate, and onPostExecute methods to insert data into the database on a background thread, display a progress dialog, and update the UI with the result. The AsyncTask downloads data from the database without blocking the main thread to ensure a responsive UI.

Uploaded by

Aditya Borle
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)
2K views3 pages

Exercise Q.1) Write A Program To Insert Data in Sqlite Database Using Asynctask. Program Code: Activity - Main - XML

The document describes how to insert data into an SQLite database using an AsyncTask in Android. It includes XML layout code for a basic UI with a button and text view. It also includes Java code for a MainActivity class that uses an AsyncTask with doInBackground, onPreExecute, onProgressUpdate, and onPostExecute methods to insert data into the database on a background thread, display a progress dialog, and update the UI with the result. The AsyncTask downloads data from the database without blocking the main thread to ensure a responsive UI.

Uploaded by

Aditya Borle
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/ 3

Exercise

Q.1) Write a program to insert data in SQLite database using AsyncTask.


 Program Code:
 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Current Location"
android:id="@+id/getLocationBtn"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/locationText"
android:layout_below="@id/getLocationBtn"/>
</RelativeLayout>

 MainActivity.java
package com.aniketbhange.practicalno26;
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;

import androidx.appcompat.app.AppCompatActivity;

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..."); // Calls
onProgressUpdate()
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) {
// execution of result of Long time consuming
operation
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]);

}
}
}

Output:

You might also like