Skip to content

Commit e6e6a94

Browse files
committed
Broadcast with otp receivers
1 parent 908a79f commit e6e6a94

File tree

11 files changed

+185
-6
lines changed

11 files changed

+185
-6
lines changed

BroadCast/app/src/main/AndroidManifest.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
package="com.broadcast.softgen.broadcast">
44

55
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
6+
<uses-permission android:name="android.permission.RECEIVE_SMS" />
7+
<uses-permission android:name="android.permission.READ_SMS" />
8+
<uses-permission android:name="android.permission.SEND_SMS" />
9+
610
<application
711
android:allowBackup="true"
812
android:icon="@mipmap/ic_launcher"
@@ -17,11 +21,20 @@
1721
<category android:name="android.intent.category.LAUNCHER" />
1822
</intent-filter>
1923
</activity>
24+
2025
<receiver android:name=".IncomingReceiver">
2126
<intent-filter>
2227
<action android:name="android.intent.action.PHONE_STATE" />
2328
</intent-filter>
2429
</receiver>
30+
<receiver
31+
android:name=".SMSReceiver"
32+
android:enabled="true"
33+
android:exported="true">
34+
<intent-filter>
35+
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
36+
</intent-filter>
37+
</receiver>
2538
</application>
2639

2740
</manifest>

BroadCast/app/src/main/java/com/broadcast/softgen/broadcast/MainActivity.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
import android.support.v7.widget.DividerItemDecoration;
1717
import android.support.v7.widget.LinearLayoutManager;
1818
import android.support.v7.widget.RecyclerView;
19+
import android.util.Log;
1920
import android.view.View;
2021
import android.widget.TextView;
2122
import android.widget.Toast;
2223

2324
import java.util.ArrayList;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
2427

2528
public class MainActivity extends AppCompatActivity {
2629
private RecyclerView recyclerView;
@@ -30,6 +33,7 @@ public class MainActivity extends AppCompatActivity {
3033
private RecycleAdapter recycleAdapter;
3134
private BroadcastReceiver broadcastReceiver;
3235
private static final int REQUEST_CODE_READ_PHONE_STATE = 1;
36+
private static final String OTP_REGEX = "[0-9]{1,6}";
3337

3438
@Override
3539
protected void onCreate(Bundle savedInstanceState) {
@@ -39,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
3943
recyclerView = (RecyclerView) findViewById(R.id.listRecycle);
4044
alertText = (TextView) findViewById(R.id.alertText);
4145
layoutManager = new LinearLayoutManager(this);
42-
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
46+
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
4347
recyclerView.setLayoutManager(layoutManager);
4448
recyclerView.setHasFixedSize(true);
4549
recycleAdapter = new RecycleAdapter(arrayList);
@@ -51,6 +55,32 @@ public void onReceive(Context context, Intent intent) {
5155
readFromDb();
5256
}
5357
};
58+
getMessages();
59+
}
60+
61+
private void getMessages() {
62+
SMSReceiver.bindListener(new SmsListener() {
63+
@Override
64+
public void messageReceived(String messageText) {
65+
66+
//From the received text string you may do string operations to get the required OTP
67+
//It depends on your SMS format
68+
Log.e("Message", messageText);
69+
Toast.makeText(MainActivity.this, "Message: " + messageText, Toast.LENGTH_LONG).show();
70+
71+
// If your OTP is six digits number, you may use the below code
72+
73+
Pattern pattern = Pattern.compile(OTP_REGEX);
74+
Matcher matcher = pattern.matcher(messageText);
75+
String otp="";
76+
while (matcher.find()) {
77+
otp = matcher.group();
78+
}
79+
80+
Toast.makeText(MainActivity.this, "OTP: " + otp, Toast.LENGTH_LONG).show();
81+
82+
}
83+
});
5484
}
5585

5686
private void readFromDb() {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.broadcast.softgen.broadcast;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.telephony.SmsMessage;
8+
9+
public class SMSReceiver extends BroadcastReceiver {
10+
private static SmsListener mListener;
11+
12+
@Override
13+
public void onReceive(Context context, Intent intent) {
14+
Bundle data = intent.getExtras();
15+
16+
Object[] pdus = (Object[]) data.get("pdus");
17+
18+
for (int i = 0; i < pdus.length; i++) {
19+
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
20+
21+
String sender = smsMessage.getDisplayOriginatingAddress();
22+
//Check the sender to filter messages which we require to read
23+
24+
if (sender.equals("97385")) {
25+
String messageBody = smsMessage.getMessageBody();
26+
//Pass the message text to interface
27+
mListener.messageReceived(messageBody);
28+
29+
}
30+
}
31+
32+
}
33+
34+
public static void bindListener(SmsListener listener) {
35+
mListener = listener;
36+
}
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.broadcast.softgen.broadcast;
2+
3+
/**
4+
* Created by DELL PC on 11-Mar-18.
5+
*/
6+
7+
public interface SmsListener {
8+
void messageReceived(String messageText);
9+
}

JavaTestMahesh/src/com/mahesh/Collections/ArrayListOfInteger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55

6+
67
public class ArrayListOfInteger {
78
public static void main(String args[]) {
89
ArrayList<Integer> arraylist = new ArrayList<Integer>();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.mahesh.abstraction;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class ElectoralVotingBallot {
7+
public static void findWinner(String votes[])
8+
{
9+
// Insert all votes in a hashmap
10+
Map<String,Integer> map =
11+
new HashMap<String, Integer>();
12+
for (String str : votes)
13+
{
14+
if (map.keySet().contains(str))
15+
map.put(str, map.get(str) + 1);
16+
else
17+
map.put(str, 1);
18+
}
19+
20+
// Traverse through map to find the candidate
21+
// with maximum votes.
22+
int maxValueInMap = 0;
23+
String winner = "";
24+
for (Map.Entry<String,Integer> entry : map.entrySet())
25+
{
26+
String key = entry.getKey();
27+
Integer val = entry.getValue();
28+
if (val > maxValueInMap)
29+
{
30+
maxValueInMap = val;
31+
winner = key;
32+
}
33+
34+
// If there is a tie, pick lexicographically
35+
// smaller.
36+
else if (val == maxValueInMap &&
37+
winner.compareTo(key) < 0)
38+
winner = key;
39+
}
40+
System.out.println("Winning Candidate is :" +
41+
winner);
42+
}
43+
44+
// Driver code
45+
public static void main(String[] args)
46+
{
47+
String[] votes = { "john", "jzzzcy", "jackie",
48+
"jzzzcy", "john", "jackie",
49+
"jamie", "jamie", "john",
50+
"jzzzcy", "jamie", "jzzzcy",
51+
"john" };
52+
53+
findWinner(votes);
54+
}
55+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mahesh.abstraction;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
public class Tesg {
7+
public static void main(String[] args) {
8+
String S = "No one could disentangle correctly";
9+
// String W[] = S.split(" ");
10+
// Arrays.sort(W, new StringLengthComparator());
11+
// String a="";
12+
// for(String str: W)
13+
// a+= str + " ";
14+
// System.out.println(a);
15+
}
16+
17+
// int fibonacci(int n) {
18+
// switch(n) {
19+
// default:
20+
// return fibonacci(n-1)+fibonacci(n-2);
21+
// case1:
22+
// case2:
23+
// }
24+
// return 1;
25+
// }
26+
}
27+
28+
class StringLengthComparator implements Comparator<String>{
29+
@Override
30+
public int compare(String str1, String str2) {
31+
return str1.length() - str2.length();
32+
}
33+
}

MyApplication/.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TestAksh/app/src/main/java/com/tecmax/testaksh/PrefManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void setRememberMe(boolean isFirstTime) {
3232
}
3333

3434
public boolean isRememberMe() {
35+
3536
return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
3637
}
3738

TestAksh/app/src/main/java/com/tecmax/testaksh/example/ChromeActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected void onCreate(Bundle savedInstanceState) {
2020
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2121
web = findViewById(R.id.website);
2222
String webSiteName = getIntent().getStringExtra("webDetails");
23-
web.setWebViewClient(new MyBrowser());
23+
web.setWebViewClient(new WebViewClient());
2424
web.getSettings().setLoadsImagesAutomatically(true);
2525
web.getSettings().setJavaScriptEnabled(true);
2626
web.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
@@ -42,6 +42,7 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
4242

4343
@Override
4444
public void onPageFinished(WebView view, String url) {
45+
4546
super.onPageFinished(view, url);
4647
}
4748
}
@@ -62,7 +63,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
6263
web.goBack();
6364
return true;
6465
} else {
65-
super.onBackPressed();
66+
super.onBackPressed();
6667
}
6768
}
6869
return super.onOptionsItemSelected(item);

0 commit comments

Comments
 (0)