Lecture 7 - Networking, SMS and Toast Notification
Lecture 7 - Networking, SMS and Toast Notification
Lecture (8)
This content has been cited from Mr. Kirolos Tharwat’s content.
Networking
2
Networking
3
Checking Network Connection
4
• Once you instantiate the object of
ConnectivityManager class, you can use
getAllNetworkInfo method to get the information of all
the networks. This method returns an array of
NetworkInfo. So you have to receive it like this.
NetworkInfo[] info = check.getAllNetworkInfo();
6
Sending SMS
•In android, we can send SMS from our android
application in two ways either by
using SMSManager API or Intent based on our
requirements.
7
1. send SMS using SMSManager API
In android, to send SMS using SMSManager API we
need to write the code like as shown below.
8
Following are the five arguments to the
sendTextMessage() method:
9
•SMSManager API required SEND_SMS permission in
our android manifest to send SMS.
10
Example to send SMS using SMSManager API
• Create a new android application using android studio and give names
as SendSMSExample.
• activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <TextView
<LinearLayout xmlns:android="http://schemas.android android:id="@+id/secTxt"
.com/apk/res/android" android:layout_width="wrap_content"
android:orientation="vertical" android:layout_width android:layout_height="wrap_content"
="match_parent" android:text="Message"
android:layout_height="match_parent"> android:layout_marginLeft="100dp" />
<TextView <EditText
android:id="@+id/fstTxt" android:id="@+id/msgTxt"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="100dp" android:layout_marginLeft="100dp"
android:layout_marginTop="150dp" android:ems="10" />
android:text="Mobile No" /> <Button
<EditText android:id="@+id/btnSend"
android:id="@+id/mblTxt" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="100dp"
android:layout_marginLeft="100dp" android:text="Send SMS" />
android:ems="10"/> </LinearLayout>
11
Example to send SMS using SMSManager API
• Now open our main activity file MainActivity.java from \src\main\java\com.
tutlane.sendsmsexample pat and write the code like as shown below
• MainActivity.java
package com.tutlane.sendsmsexample; txtMobile = (EditText)findViewById(R.id.mblTxt);
import android.content.Intent; txtMessage = (EditText)findViewById(R.id.msgTxt);
import android.net.Uri; btnSms = (Button)findViewById(R.id.btnSend);
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
btnSms.setOnClickListener(new View.OnClickListener() {
import android.os.Bundle; @Override
import android.telephony.SmsManager; public void onClick(View v) {
import android.view.View; try{
import android.widget.*; SmsManager smgr = SmsManager.getDefault();
smgr.sendTextMessage(txtMobile.getText().toStri
public ng(),null,txtMessage.getText().toString(),null,null);
class MainActivity extends AppCompatActivity {
Toast.makeText(MainActivity.this, "SMS Sent
private EditText txtMobile; Successfully", Toast.LENGTH_SHORT).show();
private EditText txtMessage; }
private Button btnSms; catch (Exception e){
@Override Toast.makeText(MainActivity.this, "SMS Failed to
Send, Please try again", Toast.LENGTH_SHORT).show();
} }
protected void onCreate(Bundle }
savedInstanceState) { });
super.onCreate(savedInstanceState); } 12
setContentView(R.layout.activity_main);
Example to send SMS using SMSManager API
• If you observe above code, we are sending SMS using SMSManager API on
button click. As discussed, we need to add a SEND_SMS permission in our
android manifest.
• Now open android manifest file (AndroidManifest.xml) and write the code like
as shown below AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <activity android:name=".MainActiv
<manifest xmlns:android="http://schemas.android.co
ity">
m/apk/res/android"
package="com.tutlane.sendsmsexample"> <intent-filter>
<uses- <action android:name="andr
permission android:name="android.permission.SEND_ oid.intent.action.MAIN" />
SMS"/>
<category android:name="an
<application
android:allowBackup="true" droid.intent.category.LAUNCHER" />
android:icon="@mipmap/ic_launcher" </intent-filter>
android:label="@string/app_name" </activity>
android:roundIcon="@mipmap/ic_launcher_roun
</application>
d"
android:supportsRtl="true" </manifest>
android:theme="@style/AppTheme">
Once you enter all details and click on Send SMS button it will send SMS and show
the alert message like as mentioned in above image. The above example we
implemented using SMSManager API 14
2. Sending SMS using Intent
• In android, Intent is a messaging object which is used to request an
action from another app component such as activities, services,
broadcast , receivers, and content providers.
• To send SMS using the Intent object, we need to write the code like as
shown below.
15
Using Implicit Intents (ACTION_VIEW)
• Using Android implicit intents we can display a list of
SMS client apps that the user already has on his phone
that he can use.
String phoneNo = "123456789";
String messageBody = "Hello SMS";
startActivity(smsIntent);
16
Using Implicit Intents (ACTION_SENDTO )
startActivity(smsIntent);
17
Receiving SMS
18
Receiving SMS
Add the following statements to the AndroidManifest.xml
file:
<application>
. . . .
<receiver android:name=”.SMSReceiver”>
<intent-filter>
<action android:name=
“android.provider.Telephony.SMS_RECEIVED” />
</intent-filter>
</receiver>
</application>
19
BroadcastReceiver
20
BroadcastReceiver
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
....
}
}
You also need to set the launchMode attribute of the <activity> element
in the AndroidManifest.xml file to singleTask:
<activity android:name=”.MainActivity”
android:label=”@string/app_name”
android:launchMode=”singleTask” >
21
Toast Notification
22
Toast Notification
• A toast notification communicates messages to the
user. These messages pop up as an overlay onto the
user’s current screen, often displaying a validation
warning message.
• Special Note:
In Android, Toast is used when we required to notify user
about an operation without expecting any user input.
It displays a small popup for message and automatically
fades out after timeout.
23
Toast Notification
Code Syntax
Thanks