0% found this document useful (0 votes)
9 views21 pages

MAD Unit 4

A content provider in Android manages access to a central data repository, allowing applications to share data securely. It provides a standard interface for CRUD operations and can be accessed using a ContentResolver object. The document outlines how to create and implement a content provider, including necessary methods and URI structure for data access.

Uploaded by

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

MAD Unit 4

A content provider in Android manages access to a central data repository, allowing applications to share data securely. It provides a standard interface for CRUD operations and can be accessed using a ContentResolver object. The document outlines how to create and implement a content provider, including necessary methods and URI structure for data access.

Uploaded by

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

Content providers

Content provider basics

A content provider manages access to a central repository of data. A provider is part of an


Android application, which often provides its own UI for working with the data. However,
content providers are primarily used by other applications, which access the provider using a
provider client object. Together, providers and provider clients offer a consistent, standard
interface to data that also handles interprocess communication and secure data access.

Typically you work with content providers in one of two scenarios: implementing code to access
an existing content provider in another application or creating a new content provider in your
application to share data with other applications.

This page covers the basics of working with existing content providers. To learn about
implementing content providers in your own applications, see Create a content provider.

This topic describes the following:

 How content providers work.


 The API you use to retrieve data from a content provider.
 The API you use to insert, update, or delete data in a content provider.
 Other API features that facilitate working with providers.

Overview

A content provider presents data to external applications as one or more tables that are similar to
the tables found in a relational database. A row represents an instance of some type of data the
provider collects, and each column in the row represents an individual piece of data collected for
an instance.

A content provider coordinates access to the data storage layer in your application for a number
of different APIs and components. As illustrated in figure 1, these include the following:

 Sharing access to your application data with other applications


 Sending data to a widget
 Returning custom search suggestions for your application through the search framework
using SearchRecentSuggestionsProvider
 Synchronizing application data with your server using an implementation
of AbstractThreadedSyncAdapter
 Loading data in your UI using a CursorLoader
Figure 1. Relationship between a content provider and other components.

Access a provider

When you want to access data in a content provider, you use the ContentResolver object in your
application's Context to communicate with the provider as a client. The ContentResolver object
communicates with the provider object, an instance of a class that implements ContentProvider.

The provider object receives data requests from clients, performs the requested action, and
returns the results. This object has methods that call identically named methods in the provider
object, an instance of one of the concrete subclasses of ContentProvider.
The ContentResolver methods provide the basic "CRUD" (create, retrieve, update, and delete)
functions of persistent storage.

A common pattern for accessing a ContentProvider from your UI uses a CursorLoader to run an
asynchronous query in the background. The Activity or Fragment in your UI calls
a CursorLoader to the query, which in turn gets the ContentProvider using the ContentResolver.

This lets the UI continue to be available to the user while the query is running. This pattern
involves the interaction of a number of different objects, as well as the underlying storage
mechanism, as illustrated in figure 2.

Figure 2. Interaction between ContentProvider, other classes, and storage.


.
Create a content provider

A content provider manages access to a central repository of data. You implement a provider as
one or more classes in an Android application, along with elements in the manifest file. One of
your classes implements a subclass of ContentProvider, which is the interface between your
provider and other applications.

Although content providers are meant to make data available to other applications, you can have
activities in your application that let the user query and modify the data managed by your
provider.

Before you start building

Before you start building a provider, consider the following:

 Decide whether you need a content provider. You need to build a content provider if you want
to provide one or more of the following features:
 You want to offer complex data or files to other applications.
 You want to let users copy complex data from your app into other apps.
 You want to provide custom search suggestions using the search framework.
 You want to expose your application data to widgets.
 You want to implement the AbstractThreadedSyncAdapter, CursorAdapter,
or CursorLoader classes.
You don't need a provider to use databases or other types of persistent storage if the use is
entirely within your own application and you don’t need any of the preceding features listed.
Instead, you can use one of the storage systems described in Data and file storage overview.
 If you haven't done so already, read Content provider basics to learn more about providers and
how they work.
Follow these steps to build your provider:
1. Design the raw storage for your data. A content provider offers data in two ways:

File data

Data that normally goes into files, such as photos, audio, or videos. Store the files in your
application's private space. In response to a request for a file from another application,
your provider can offer a handle to the file.

"Structured" data
Data that normally goes into a database, array, or similar structure. Store the data in a
form that's compatible with tables of rows and columns. A row represents an entity, such
as a person or an item in inventory. A column represents some data for the entity, such a
person's name or an item's price. A common way to store this type of data is in a SQLite
database, but you can use any type of persistent storage. To learn more about the storage
types available in the Android system, see the Design data storage section.
2. Define a concrete implementation of the ContentProvider class and its required methods. This
class is the interface between your data and the rest of the Android system. For more information
about this class, see the Implement the ContentProvider class section.
3. Define the provider's authority string, content URIs, and column names. If you want the
provider's application to handle intents, also define intent actions, extras data, and flags. Also
define the permissions that you require for applications that want to access your data. Consider
defining all these values as constants in a separate contract class. Later, you can expose this class
to other developers. For more information about content URIs, see the Design content
URIs section. For more information about intents, see the Intents and data access section.
4. Add other optional pieces, such as sample data or an implementation
of AbstractThreadedSyncAdapter that can synchronize data between the provider and cloud-
based data.
Content URI
Content URI(Uniform Resource Identifier) is the key concept of Content providers. To
access the data from a content provider, URI is used as a query string.
Structure of a Content URI: content://authority/optionalPath/optionalID
Details of different parts of Content URI:
 content:// – Mandatory part of the URI as it represents that the given URI is a Content
URI.
 authority – Signifies the name of the content provider like contacts, browser, etc. This part
must be unique for every content provider.
 optionalPath – Specifies the type of data provided by the content provider. It is essential
as this part helps content providers to support different types of data that are not related to
each other like audio and video files.
 optionalID – It is a numeric value that is used when there is a need to access a particular
record.
If an ID is mentioned in a URI then it is an id-based URI otherwise a directory-based
URI.
For a deeper understanding of how to implement Content Providers and other critical Android
components using Kotlin, consider enrolling in the Android Development with Kotlin
course . This course is designed to equip you with the skills necessary to build advanced
Android applications, leveraging Kotlin’s features to enhance your development process.
Operations in Content Provider
Four fundamental operations are possible in Content Provider
namely Create , Read , Update , and Delete . These operations are often termed as CRUD
operations .
 Create: Operation to create data in a content provider.
 Read: Used to fetch data from a content provider.
 Update: To modify existing data.
 Delete: To remove existing data from the storage.
Working of the Content Provider
UI components of android applications like Activity and Fragments use an
object CursorLoader to send query requests to ContentResolver. The ContentResolver object
sends requests (like create, read, update, and delete) to the ContentProvider as a client. After
receiving a request, ContentProvider process it and returns the desired result. Below is a
diagram to represent these processes in pictorial form.

Creating a Content Provider


Following are the steps which are essential to follow in order to create a Content Provider:
 Create a class in the same directory where the that MainActivity file resides and this class
must extend the ContentProvider base class.
 To access the content, define a content provider URI address.
 Create a database to store the application data.
 Implement the six abstract methods of ContentProvider class.
 Register the content provider in AndroidManifest.xml file using <provider> tag .
Following are the six abstract methods and their description which are essential to
override as the part of ContenProvider class:
Abstract Method Description

query() A method that accepts arguments and fetches the data from the
desired table. Data is retired as a cursor object.

insert() To insert a new row in the database of the content provider.


It returns the content URI of the inserted row.

update() This method is used to update the fields of an existing row.


It returns the number of rows updated.

delete() This method is used to delete the existing rows.


It returns the number of rows deleted.
Abstract Method Description

getType() This method returns the Multipurpose Internet Mail Extension(MIME)


type of data to the given Content URI.

onCreate() As the content provider is created, the android system calls


this method immediately to initialise the provider.

Example
The prime purpose of a content provider is to serve as a central repository of data where users
can store and can fetch the data. The access of this repository is given to other applications also
but in a safe manner in order to serve the different requirements of the user. The following are
the steps involved in implementing a content provider. In this content provider, the user can
store the name of persons and can fetch the stored data. Moreover, another application can also
access the stored data and can display the data.
Note: Following steps are performed on Android Studio version 4.0
Creating a Content Provider:
Step 1: Create a new project
1. Click on File, then New => New Project.
2. Select language as Java/Kotlin.
3. Choose empty activity as a template
4. Select the minimum SDK as per your need.
Step 2: Modify the strings.xml file
All the strings used in the activity are stored here.
<resources>
<string name="app_name">Content_Provider_In_Android</string>
<string name="hintText">Enter User Name</string>
<string name="heading">Content Provider In Android</string>
<string name="insertButtontext">Insert Data</string>
<string name="loadButtonText">Load Data</string>
</resources>
Step 3: Creating the Content Provider class
1. Click on File, then New => Other => ContentProvider.
2. Name the ContentProvider
3. Define authority (it can be anything for example “com.demo.user.provider” )
4. Select Exported and Enabled option
5. Choose the language as Java/Kotlin
This class extends the ContentProvider base class and override the six abstract methods. Below
is the complete code to define a content provider.
package com.example.contentprovidersinandroid;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import java.util.HashMap;

public class MyContentProvider extends ContentProvider {


public MyContentProvider() {
}

// defining authority so that other application can access it


static final String PROVIDER_NAME = "com.demo.user.provider";

// defining content URI


static final String URL = "content://" + PROVIDER_NAME + "/users";

// parsing the content URI


static final Uri CONTENT_URI = Uri.parse(URL);

static final String id = "id";


static final String name = "name";
static final int uriCode = 1;
static final UriMatcher uriMatcher;
private static HashMap<String, String> values;

static {

// to match the content URI


// every time user access table under content provider
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

// to access whole table


uriMatcher.addURI(PROVIDER_NAME, "users", uriCode);

// to access a particular row


// of the table
uriMatcher.addURI(PROVIDER_NAME, "users/*", uriCode);
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case uriCode:
return "vnd.android.cursor.dir/users";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
// creating the database
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
if (db != null) {
return true;
}
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(TABLE_NAME);
switch (uriMatcher.match(uri)) {
case uriCode:
qb.setProjectionMap(values);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder == "") {
sortOrder = id;
}
Cursor c = qb.query(db, projection, selection, selectionArgs, null,
null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}

// adding data to the database


@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = db.insert(TABLE_NAME, "", values);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLiteException("Failed to add a record into " + uri);
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case uriCode:
count = db.update(TABLE_NAME, values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)) {
case uriCode:
count = db.delete(TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}

// creating object of database


// to perform query
private SQLiteDatabase db;

// declaring name of the database


static final String DATABASE_NAME = "UserDB";

// declaring table name of the database


static final String TABLE_NAME = "Users";

// declaring version of the database


static final int DATABASE_VERSION = 1;

// sql query to create the table


static final String CREATE_DB_TABLE = " CREATE TABLE " + TABLE_NAME
+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " name TEXT NOT NULL);";

// creating a database
private static class DatabaseHelper extends SQLiteOpenHelper {

// defining a constructor
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// creating a table in the database


@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_DB_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// sql query to drop a table


// having similar name
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}

Step 4: Design the activity_main.xml layout


One Textview , EditText field, two Buttons , and a Textview to display the stored data will
be added in the activity using the below code.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:background="#168BC34A"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.13"
tools:ignore="MissingConstraints">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginBottom="70dp"
android:fontFamily="@font/roboto"
android:text="@string/heading"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/holo_green_dark"
android:textSize="36sp"
android:textStyle="bold" />

<EditText
android:id="@+id/textName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
android:fontFamily="@font/roboto"
android:hint="@string/hintText" />

<Button
android:id="@+id/insertButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#4CAF50"
android:fontFamily="@font/roboto"
android:onClick="onClickAddDetails"
android:text="@string/insertButtontext"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<Button
android:id="@+id/loadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#4CAF50"
android:fontFamily="@font/roboto"
android:onClick="onClickShowDetails"
android:text="@string/loadButtonText"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#FFFFFF"
android:textStyle="bold" />

<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:clickable="false"
android:ems="10"
android:fontFamily="@font/roboto"
android:textColor="@android:color/holo_green_dark"
android:textSize="18sp"
android:textStyle="bold" />

</LinearLayout>

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/banner" />
</androidx.constraintlayout.widget.ConstraintLayout>

Step 5: Modify the MainActivity file


Button functionalities will be defined in this file. Moreover, the query to be performed while
inserting and fetching the data is mentioned here. Below is the complete code.
JavaKotlin
package com.example.contentprovidersinandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
public void onClickAddDetails(View view) {

// class to add values in the database


ContentValues values = new ContentValues();

// fetching text from user


values.put(MyContentProvider.name, ((EditText)
findViewById(R.id.textName)).getText().toString());
// inserting into database through content URI
getContentResolver().insert(MyContentProvider.CONTENT_URI, values);

// displaying a toast message


Toast.makeText(getBaseContext(), "New Record Inserted",
Toast.LENGTH_LONG).show();
}

public void onClickShowDetails(View view) {


// inserting complete table details in this text field
TextView resultView= (TextView) findViewById(R.id.res);

// creating a cursor object of the


// content URI
Cursor cursor = getContentResolver().query(Uri.parse("content://com.demo.user.provider/
users"), null, null, null, null);

// iteration of the cursor


// to print whole table
if(cursor.moveToFirst()) {
StringBuilder strBuild=new StringBuilder();
while (!cursor.isAfterLast()) {
strBuild.append("\n"+cursor.getString(cursor.getColumnIndex("id"))+ "-"+
cursor.getString(cursor.getColumnIndex("name")));
cursor.moveToNext();
}
resultView.setText(strBuild);
}
else {
resultView.setText("No Records Found");
}
}
}

Step 6: Modify the AndroidManifest file


The AndroidManifest file must contain the content provider name, authorities, and permissions
which enable the content provider to be accessed by other applications.
XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.content_provider_in_android">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="com.example.contentprovidersinandroid.MyContentProvider"
android:authorities="com.demo.user.provider"
android:enabled="true"
android:exported="true"></provider>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>

Sending a Text Message Over the Phone Using SmsManager in Android


What is the use of the SmsManager Class?
SMSManager class manages operations like sending text messages, data messages, and
multimedia messages (MMS). For sending a text message method sendTextMessage() is used
likewise for multimedia message sendMultimediaMessage() and for data message,
the sendDataMessage() method is used.
The details of each function are:
Function Description

sendTextMessage(String destinationAddress, String


sendTextMessage() scAddress, String text, PendingIntent
sentIntent, PendingIntent deliveryIntent, long messageId)

sendDataMessage(String destinationAddress, String


sendDataMessage() scAddress, short destinationPort, byte[] data, PendingIntent
sentIntent, PendingIntent deliveryIntent)
Function Description

sendMultimediaMessage(Context context, Uri contentUri,


sendMultimediaMessage() String locationUrl, Bundle configOverrides, PendingIntent
sentIntent

Below is an example of a basic application that sends a text message.

Step-by-Step Implementation of Text Message over the Phone


Step 1: Create a new Android Application.
Step 2: Go to AndroidManifest.xml.
app->Manifest->AndroidManifest.xml
Step 3: In AndroidManifest.xml add the permission to send SMS. It will permit an android
application to send SMS.
<uses-permission android:name=" android.permission.SEND_SMS " />

Step 4: Open activity_main.xml from the following address and add the below code. Here, in a
Linear Layout, two edittext for taking phone number and a text message and a button for
sending the message is added.
app->res->layout->activitymain.xml
Below is the code implementation of the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<ImageView
android:id="@+id/imageView"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="60dp"
android:src="@drawable/gfg_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number:"
android:width="165sp"
android:textSize="18sp" />

<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter number"
android:inputType="phone" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message to be Sent:"
android:width="165sp"
android:textSize="18sp" />

<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="top|start"
android:hint="Enter message"
android:inputType="textMultiLine" />
</LinearLayout>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="32dp"
android:text="SEND" />

</LinearLayout>

Step 5: Working on MainActivity.java


app->java->com.example.gfg->MainActivity

Create objects for views used i.e., editTexts and button. In the onCreate method find all the
views using the findViewById() method.

Viewtype object =(ViewType)findViewById(R.id.IdOfView);


Since the Send button is for sending the message so onClickListener is added with the button.
Now create two string variables and store the value of editText phone number and message into
them using method getText() (before assigning convert them to a string
using toString() method). Now in try block create an instance of SMSManager class and get the
SMSManager associated with the default subscription id. Now call
method sendTextMessage() to send message.
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(number,null,msg,null,null);
Then display the toast message as “Message sent ” and close the try block. At last in catch block
display a toast message because the message was not sent if the compiler executes this block of
the code.

MainActivity.java

package com.example.gfg;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText phonenumber,message;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send=findViewById(R.id.button);
phonenumber=findViewById(R.id.editText);
message=findViewById(R.id.editText2);
send.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {


String number=phonenumber.getText().toString();
String msg=message.getText().toString();
try {
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(number,null,msg,null,null);
Toast.makeText(getApplicationContext(),"Message
Sent",Toast.LENGTH_LONG).show();
}catch (Exception e)
{
Toast.makeText(getApplicationContext(),"Some fields is
Empty",Toast.LENGTH_LONG).show();
}
}
});
}
}

For running application in Android device enable the SMS permission for the app.
Goto permissions->SMS->YourApp and enable permission.

Sent message in the messaging app


How to Send an Email From an Android Application?
In this article, you will make a basic Android Application that can be used to send email
through your android application. You can do so with the help of Intent with action
as ACTION_SEND with extra fields:
 email id to which you want to send mail,
 the subject of the email and
 body of the email.
Basically Intent is a simple message object that is used to communicate between android
components such as activities, content providers, broadcast receivers, and services, here
use to send the email. This application basically contains one activity with EditText to take
input of email address, subject, and body of the email from the user and button to send that
email.
Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project
in Android Studio. The code for that has been given in both Java and Kotlin Programming
Language for Android.
Step 2: Working with the XML Files
Next, go to the activity_main.xml file, which represents the UI of the project. Below is the
code for the activity_main.xml file. Comments are added inside the code to understand the
code in more detail. This file contains a Relative Layout which contains three Edit texts for
receiver mail id, another for the subject of the mail, and last one for the body of the email, and
three TextViews for the label and a button for starting intent or sending mail:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy