Automatically SMS OTP reader using Broadcast Receiver in android studio
Hi Guys, Welcome to Proto Coders Point, In this Android Tutorial we will build an android application that will be able in reading incoming message automatically in android to verify otp.
FINAL OUTPUT OF READING OTP AUTOMATICALLY
Video Tutorial on How to auto read otp in android using Breadcast Receiver.
At the end of this android tutorial your app will be able the read sms and fetch all the text in that sms and then it will copy the OTP from SMS and paste it in EditText box.
So let’s begin implementation of otp sms reader
Create a new Android Project
Ofcourse you need to create a new android project or open any existing android project
To do so go to File > New > New Project
Give a name as per you need in my case i have names my application as “Auto sms otp reader”
Create a custom background for EditText and a Vector Image
1. edittextbackground.xml
create a new xml file in drawable folder
res > drawable(right click) > New > Drawable resource file
a dialog box will appear on your screen as shown below
Then, Here you can select any kind of image clip Art.
Create a new Java Class names OTPReceiver
OTPReceiver.java
package com.example.autosmsotpreader;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.widget.EditText;
public class OTP_Receiver extends BroadcastReceiver {
private static EditText editText;
public void setEditText(EditText editText)
{
OTP_Receiver.editText=editText;
}
// OnReceive will keep trace when sms is been received in mobile
@Override
public void onReceive(Context context, Intent intent) {
//message will be holding complete sms that is received
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for(SmsMessage sms : messages)
{
String msg = sms.getMessageBody();
// here we are spliting the sms using " : " symbol
String otp = msg.split(": ")[1];
editText.setText(otp);
}
}
}
eg: sms received is Your OTP is : 4587, Then in above code i have split the complete message string in to 2 parts using “:” this special symbol.
Open AndroidManifest.xml file & add the uses permission
adding uses permission
add this 4 user-permission inside <manifest> here </manifest> tags
<!-- this are the permission need so that application can read sms -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
Now, Add the following receiver OTPReceiver.java in manifest under <application> tag
Hi Guys, Welcome to Proto Coder Point, In this Flutter Tutorial will use Shimmer Effect flutter library to show/display loading content effect before showing the actual data.
To achieve this we will make use of Flutter Package Called shimmer
A Shimmer Effect is just an shine effect on any of the View like text, images or any widget.
Shimmer is one of the flutter package library that is one of the easiest way to add an shimmer effect to any widget in flutter.
Now a day it is mostly used as loading indicator, i,e is to show loading content effect.
Video Tutorial
FINAL OUTPUT
Ok let’s begin adding the Library in our flutter project
Step 1: Create a new Flutter Project
I am making use of android studio as my IDE to build Flutter Application
Create new Flutter project > Give a name as “Flutter Shimmer Effect”
or if you already have existing flutter project then just open it.
Step 2: Add the Required Dependencies
On the Left side you may see project section navigate towords it and just open pubspec.yaml file and add the following shimmer library dependencies as show below
dependencies:
shimmer: ^1.1.1 #library for Shimmer effect
after adding the dependencies you need to click on “pub get”,what this does is, it will download all the required package class file into your flutter project.
Step 3: Import the shimmer.dart package
Once you have added the dependencies in your project now ou can easily use those library classes just by importing the shimmer.dart file, wherever required.
In above Snippet code i have made a sized box with width as screen size available using MediaQuery to get Screen size and Height of box manually specified as 400.0 px
we have some properties that can we used to show shimmer effect:
baseColor: which you can assume as a background color of shimmer effect
highlightColor : which will show you shimmer animation effect, some thing like it is loading content from internet.
direction : you can change the direction of the shimmer highlight color from left to right (ltr) , right to left (rtf), top to bottom (ttb) or bottom to top (btt) , to do so you just need to pass ShimmerDirection with specified direction.
Complete Source code of Flutter Shimmer Effect with loading a image after 5 seconds using Future.delayed function
main.dart
Copy paste the below linked of flutter dart code in main.dart file
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool showImageWidget = false;
@override
void initState() {
// TODO: implement initState
super.initState();
// create a future delayed function that will change showInagewidget to true after 5 seconds
Future.delayed(const Duration(seconds: 5), () {
setState(() {
showImageWidget = true;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Shimmer Effect Example"),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
//if show image widget is true then image will be displayed
// is show image widgte is false then loading shimmer effect will be show for 5 seconds
showImageWidget
? Image.network(
"https://i.pinimg.com/564x/1b/30/80/1b30806bed30a7d071752948d00e75f8.jpg",
width: MediaQuery.of(context).size.width,
height: 400.0,
fit: BoxFit.fill,
)
: SizedBox(
width: MediaQuery.of(context).size.width,
height: 400.0,
child: Shimmer.fromColors(
child: Card(
color: Colors.grey,
),
baseColor: Colors.white70,
highlightColor: Colors.grey[700],
direction: ShimmerDirection.ltr,
)),
Text(
"Shimmer Effect Example in flutter using Shimmer Flutter Library",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20),
)
],
),
),
);
}
}
This Code is just for demo purpose
Here i have set a boolean value for showImageWidget = false
then, i have used a Future.delayed() function that will wait for 5 seconds then change showImageWidget = true,
So when showImageWidget is false app will display shimmer loading effect and when showImageWidget becomes true after 5 second Image widget will be loaded on screen.
Introduction to Curved Bottom Navigation bar Flutter Library
By using this flutter package library you can easily create a Home Page/MainPage with Animated Bottom Navigation bar, This Flutter UI Library is very easy to implementation of curved navigation bar.
DEMO ON FINAL RESULT OF THIS FLUTTER TUTORIAL
Adding this Curved Bottom Navigation bar library in our flutter Project
Step 1: Adding the Dependencies
dependencies:
curved_navigation_bar: ^0.3.2 #latest version
Once, you add the dependencies you need to click on pub get button/text, What it does is it download all the required classes or library in you flutter project.
Step 2: Importing the library wherever required to use
for example: if you want to add curved bottom navigation bar in main page of your flutter application, then open main.dart file and add the import package line on top.
To study more about Connectivity Library in Flutter then visit here
Introduction to Flutter Connectivity Library
This Plugin is very useful if your app need internet connection to run the application perfectly, This Library allows your flutter Application to Discover Network Connectivity. This Flutter Library will also check if your mobile is currently using cellular mobile data or is using WiFi Connection.
This Flutter Plugin Perfectly works for Both Android and iOS devices, So it is been rated with 100 points in Flutter Library Store.
Then, let’s Start with adding this library into our Flutter Project.
Step 1: Adding Dependencies
Open pubspec.yaml file and add the following dependencies
dependencies:
connectivity: ^0.4.8+2 // add this line
After adding the dependencies just hit Pub Get, This will download all the required packages of Flutter Connectivity library into your flutter project.
Step 2 : Import the Class plackage
Then, After adding the dependencies just you need to do is import the class package wherever it’s required.
import 'package:connectivity/connectivity.dart';
Step 3 : Snippet code How to use function or method from connectivity library
How to detect internet connection in flutter.
if internet connected, then Weather it mobile Data or Wifi Connection.
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
// Mobile is not Connected to Internet
}
else if (connectivityResult == ConnectivityResult.mobile) {
// I am connected to a mobile network.
}
else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
}
If mobile connectivity is Wifi then, This is How to get Wifi details in flutter.
var wifiBSSID = await (Connectivity().getWifiBSSID());
var wifiIP = await (Connectivity().getWifiIP());network
var wifiName = await (Connectivity().getWifiName());wifi network
You can Also keep checking OnConnectivityChanged Like this :
@override
initState() {
super.initState();
subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
// Got a new connectivity status!
})
}
// Be sure to cancel subscription after you are done
@override
dispose() {
super.dispose();
subscription.cancel();
}
Complete Source Code – How to check if internet is connected in Flutter?
main.dart
Copy Paste Below Lines of Flutter Code in main.dart file
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeConnect(),
);
}
}
class HomeConnect extends StatefulWidget {
@override
_HomeConnectState createState() => _HomeConnectState();
}
class _HomeConnectState extends State<HomeConnect> {
var wifiBSSID;
var wifiIP;
var wifiName;
bool iswificonnected = false;
bool isInternetOn = true;
@override
void initState() {
// TODO: implement initState
super.initState();
GetConnect(); // calls getconnect method to check which type if connection it
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Connectivity..."),
),
body: isInternetOn
? iswificonnected ? ShowWifi() : ShowMobile()
: buildAlertDialog(),
);
}
AlertDialog buildAlertDialog() {
return AlertDialog(
title: Text(
"You are not Connected to Internet",
style: TextStyle(fontStyle: FontStyle.italic),
),
);
}
Center ShowWifi() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
" Your are connected to ${iswificonnected ? "WIFI" : "MOBILE DATA"}"),
Text(iswificonnected ? "$wifiBSSID" : "Not Wifi"),
Text("$wifiIP"),
Text("$wifiName")
],
),
);
}
Center ShowMobile() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(" Your are Connected to MOBILE DATA"),
],
),
);
}
void GetConnect() async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
setState(() {
isInternetOn = false;
});
} else if (connectivityResult == ConnectivityResult.mobile) {
iswificonnected = false;
} else if (connectivityResult == ConnectivityResult.wifi) {
iswificonnected = true;
setState(() async {
wifiBSSID = await (Connectivity().getWifiBSSID());
wifiIP = await (Connectivity().getWifiIP());
wifiName = await (Connectivity().getWifiName());
});
}
}
}
Hi Guys, Welcome to Proto Coders Point, In this android Tutorial we will make use of a StoryView android library developed by Ankit Kumar bxute to show stories like social media.
Hi Guys, Welcome to Proto Coders Point, This is PART 3 of WhatsApp Clone UI using Flutter, In this part we will continue with cloning design for Call Tab Page of WhatsApp.
If you have not gone through the previous part of WhatsApp UI Clone using Flutter, Then make sure to go through it.