Skip to content

Auto read sms otp #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#This is a gitignore file
#Created by "rohitss"
/*/.gradle
/*/.idea/
/*/app/build/
/*/app/src/androidTest
/*/app/src/test
/*/app/*.iml
/*/build
/*/*.iml
/*/import-summary.txt
/*/local.properties
/*/.idea/workspace.xml
/*/.idea/libraries
/*.DS_Store
/*/captures
16 changes: 16 additions & 0 deletions AutoReadSmsOtp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#This is a gitignore file
#Created by "rohitss"
/.gradle
/.idea/
/app/build/
/app/src/androidTest
/app/src/test
/app/*.iml
/build
/*.iml
/import-summary.txt
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/captures
26 changes: 26 additions & 0 deletions AutoReadSmsOtp/AutoReadSmsOtp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
* Steps:
* 1. Add Permissions to manifest
* * <uses-permission android:name="android.permission.READ_SMS" />
* * <uses-permission android:name="android.permission.RECEIVE_SMS" />
* <p>
* 2. Register our broadcast listener in manifest
* * <receiver
* * android:name=".SmsReceiver"
* * android:permission="android.permission.BROADCAST_SMS">
* * <intent-filter>
* * <action android:name="android.provider.Telephony.SMS_RECEIVED" />
* * </intent-filter>
* * </receiver>
* <p>
* 3. Add two classes to your project.
* * SmsListener.class
* * SmsReceiver.class
** <p>
*
* 4. In your MainActivity.class or activity where you want to receive sms/otp, add the existing code
* * (You can find the code in MainActivity.class file i.e. same file)
* *
*
* Note: In SmsReceiver.class there is a constant which is used to distinguish all sms senders from our sender
* Change this constant according to your project
* * private static final String SMS_SENDER_NAME = "12345";
1 change: 1 addition & 0 deletions AutoReadSmsOtp/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
30 changes: 30 additions & 0 deletions AutoReadSmsOtp/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.rohitss.autoreadsmsotp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
// testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// exclude group: 'com.android.support', module: 'support-annotations'
// })
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
// testCompile 'junit:junit:4.12'
}
17 changes: 17 additions & 0 deletions AutoReadSmsOtp/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\Soft_Development\Android\Android_SDK/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
30 changes: 30 additions & 0 deletions AutoReadSmsOtp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rohitss.autoreadsmsotp">

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.rohitss.autoreadsmsotp;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

/**
* This is the Activity class where we want to receive the sms/otp.
* Created by rohitss.
*/
public class MainActivity extends AppCompatActivity {
private static final int READ_SMS_PERMISSION_CODE = 101;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
//Request Permission for SMS
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
//Permission is not allowed so request permission.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, READ_SMS_PERMISSION_CODE);
} else {
//Permission is allowed so proceed.
autoReadSmsOtp();
}
}

/**
* This is the function where we can read received SMS.
*/
private void autoReadSmsOtp() {
SmsReceiver.isAutoReadOtp = true;
SmsReceiver.bindListener(new SmsListener() {
@Override
public void messageReceived(String messageText) {
Toast.makeText(getBaseContext(), messageText, Toast.LENGTH_LONG).show();
}
});
}

@Override
protected void onResume() {
super.onResume();
//Enable our broadcast receiver.
ComponentName receiver = new ComponentName(mContext, SmsReceiver.class);
PackageManager pm = mContext.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

@Override
protected void onPause() {
super.onPause();
//Disable our broadcast receiver.
ComponentName receiver = new ComponentName(mContext, SmsReceiver.class);
PackageManager pm = mContext.getPackageManager();
pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

/**
* This function is used check if permission is allowed or not.
*
* @param requestCode requestCode
* @param permissions permissions
* @param grantResults grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//TODO: Permissions Received, auto read otp functionality will work now
autoReadSmsOtp();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_SMS)) {
//Show permission explanation dialog...
Snackbar.make(findViewById(android.R.id.content), "Permission required to receive OTP.", Snackbar.LENGTH_LONG)
.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, READ_SMS_PERMISSION_CODE);
}
})
.setActionTextColor(ContextCompat.getColor(mContext, R.color.colorAccent))
.show();
} else {
//Never ask again selected, or device policy prohibits the app from having that permission.
//So, disable that feature, or fall back to another situation...
//Open App Settings Page
Snackbar.make(findViewById(android.R.id.content), "You have denied this permission. Please allow this permission.", Snackbar.LENGTH_LONG)
.setAction("Settings", new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentSettings = new Intent();
intentSettings.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intentSettings.addCategory(Intent.CATEGORY_DEFAULT);
intentSettings.setData(Uri.parse("package:" + mContext.getPackageName()));
intentSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentSettings.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intentSettings.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
mContext.startActivity(intentSettings);
}
})
.setActionTextColor(ContextCompat.getColor(mContext, R.color.colorAccent))
.show();
}
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.rohitss.autoreadsmsotp;

/**
* This is the Interface to listen to received sms.
* Created by rohitss.
*/
public interface SmsListener {
void messageReceived(String strMessageBody);
}
Loading