why is my SMS verification code not working properly? - android

I think I'm in a bit of a catch 22 situation. I'm making an app that verifies the user's phone number by sending a text message to the number the user entered and then checking - that way it's definitely the user's phone number. This activity should only appear once, at the beginning - for the user to verify, and from then on activity 2 should be loaded.
I am trying to do this with :
// when the form loads, check to see if phoneNo is in there
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
// if it is in there, start the new Activity
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
But it doesn't seem to be working properly. For a start, I'm not even sure it should be null. When I turn off my phone and on again, it starts back at activity 1, asking for verification.
And I can't test it. I need to run the app on my device as I can't send an SMS from the emulator, and I'm unable to get to the files where sharedPreferences exist on my device, so I can't even see if it's being written correctly.
Any advice on how I might be able to see the MyData.xml on my phone and proceed with testing my app, or tell my how I may be able to improve my code ? I downloaded the Astro app but still couldn't find it.
Here's my code :
package com.example.chris.tutorialspoint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.tutorialspoint.R;
public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
String phoneNo;
String origNumber;
private BroadcastReceiver receiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editText);
// when the form loads, check to see if phoneNo is in there
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
// if it is in there, start the new Activity
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
//if it is not in there, proceed with phone number verification
else {
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
IntentFilter filter = new IntentFilter();
// the thing we're looking out for is received SMSs
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
origNumber = msg.getOriginatingAddress();
Toast.makeText(getApplicationContext(), "Originating number" + origNumber, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Sent to number" + phoneNo, Toast.LENGTH_LONG).show();
}
};
registerReceiver(receiver, filter);
}
}
protected void sendSMSMessage() {
phoneNo = txtphoneNo.getText().toString();
//this is the SMS received
String message = "Verification test code. Please ignore this message. Thank you.";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
//if originating phone number is the same as the sent to number, save
//and go to the next activity
if (origNumber.equals(phoneNo)) {
//save the phone number
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("phonenumber", phoneNo);
editor.commit();
Intent myIntent = new Intent(MainActivity.this, PopulistoContactList.class);
MainActivity.this.startActivity(myIntent);
}
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
#Override
protected void onDestroy() {
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onDestroy();
}

What do you expect to happen here:
String phoneNoCheck = sharedPreferences.getString("phonenumber","");
if (phoneNoCheck != null) {
...
}
The method that you are calling, getString(String, String), uses the second parameter as the default value, that is, if no property is found, it will default to, as you have defined, an empty string, "".
You then try and compare this to null, which will never be equal.
Try changing it up to something like this:
String phoneNoCheck = sharedPreferences.getString("phonenumber", null);
// check if null or empty
if ( null == phoneNoCheck || phoneNoCheck.equals("") ) {
// unregistered
}

Related

smsManager does not send SMS and it's not requesting permission in the app although I have requested in the code

I am relatively new to android studio and I have some issues with smsManager. It does not request permissions for SMS when i launch the app and doesn't send SMS. The code has no errors either. This is my code:
ActivityCompat.requestPermissions(ViolationActivity.this, new String[]{Manifest.permission.SEND_SMS},PackageManager.PERMISSION_GRANTED);
public void sendSMS() {
String /*message = IDNumber.getText().toString();
message = violationPicker.getSelectedItem().toString();*/
message = additional_info.getText().toString();
String number = PhoneNumber.getText().toString();
SmsManager mySmsmanager = SmsManager.getDefault();
mySmsmanager.sendTextMessage(number, null, message, null, null);
Button btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(v->sendSMS());
}
All necessary permissions are requested in the manifest.
You are calling button click inside the sendSms method.
I have done what you wanted to achieve and it works.
Here is my code snippet.
First ensure you have the following permission in your manifest file.
<uses-permission android:name="android.permission.SEND_SMS"/>
Then in your activity have the following method
public void sendSMS(String message, String number) {
SmsManager mySmsmanager = SmsManager.getDefault();
mySmsmanager.sendTextMessage(number, null, message, null, null);
}
Request permission and send message and number to your sendSms method when user clicks the button.
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, PackageManager.PERMISSION_GRANTED);
Button btnSubmit = findViewById(R.id.btnSubmit);
EditText edMessage = findViewById(R.id.edMessage);
EditText edNumber = findViewById(R.id.edPhoneNumber);
//listen to button click
btnSubmit.setOnClickListener(view -> {
//get message and number
String message = edMessage.getText().toString();
String number = edNumber.getText().toString();
//pass your message and number to sendSms method
sendSMS(message, number);
});
The whole snippet:
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class YourActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, PackageManager.PERMISSION_GRANTED);
Button btnSubmit = findViewById(R.id.btnSubmit);
EditText edMessage = findViewById(R.id.edMessage);
EditText edNumber = findViewById(R.id.edPhoneNumber);
//listen to button click
btnSubmit.setOnClickListener(view -> {
//get message and number
String message = edMessage.getText().toString();
String number = edNumber.getText().toString();
//pass your message and number to sendSms method
sendSMS(message, number);
});
}
public void sendSMS(String message, String number) {
SmsManager mySmsmanager = SmsManager.getDefault();
mySmsmanager.sendTextMessage(number, null, message, null, null);
}
}

Using shared preference to save the string and send the saved string as sms to another number

I have to send my own mobile number as sms to another number say xxx i used getLine1number() function it is returning null since it does not have the number stored in the sim.
I am trying to Enter my number save it once using shared preference and send that number as sms to another number.. how it is possible?My code is
package com.example.mobile;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
public class MainActivity extends Activity {
EditText editText;
Button saveButton;
String mStoredNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
saveButton = (Button) findViewById(R.id.button1);
saveButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
savePreferences("storedNumber", editText.getText().toString());
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
// Load number and send to phoneNo
loadSavedPreferences();
// Now the stored number is in mStoredNumber
String phoneNo = "123";
TelephonyManager tManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String Imsi=tManager.getSubscriberId();
String Imei=tManager.getDeviceId();
try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, "IMSI,IMEI and phone number - "+Imsi+","+Imei+","+mStoredNumber+"", null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
loadSavedPreferences();
}
});
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String number = sharedPreferences.getString("storedNumber", "YourNumber");
mStoredNumber = number;
editText.setText(number);
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
}
My xml code is
<?xml version="1.0"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/enter_your_number_"
android:inputType="number">
<requestFocus />
</EditText>
<Button android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/save"/>
</LinearLayout>
The number is getting saved and when i again open the application it is not showing the number on the top its again asking for enter the number. And second time when i open the app i should not get the save button.
Please suggest a solution.
EDIT:
If you want this to happen without pressing the button, just move the SMS-sending part to onCreate.
MainActivity.java
public class MainActivity extends Activity {
EditText editText;
Button saveButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find editText and saveButton
editText = (EditText) findViewById(R.id.editText1);
saveButton = (Button) findViewById(R.id.button1);
// Try to send existing number via SMS
if (sendSMS()) {
// Successful, exit the app
finish();
}
// If we are here, this means sendSMS returned false
// if the phone number was saved before, it is now displayed in editText
// add an onClickListener to the save button
saveButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// Save number and send it
savePhoneNumber(editText.getText().toString());
sendSMS();
}
});
}
/* Sends saved phone number + IMEI, IMSI via SMS, returns true if successful */
private boolean sendSMS() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String number = sharedPreferences.getString("storedNumber", "");
if (number == "") {
// No saved number, ask user to enter it and save it
Toast.makeText(this, "Enter your phone number and tap on Save", Toast.LENGTH_SHORT).show();
return false;
}
else {
// There is saved phone number, add it to editText for later use (if SMS sending fails)
editText.setText(number);
String phoneNo = "123";
TelephonyManager tManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String Imsi=tManager.getSubscriberId();
String Imei=tManager.getDeviceId();
try{
SmsManager smsManager = SmsManager.getDefault();
String message = "IMSI, IMEI and phone number: "+Imsi+", "+Imei+", "+number;
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
return true;
}
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS failed, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
return false;
}
}
}
/* Stores phone number in the default shared prefs. */
private void savePhoneNumber(String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString("storedNumber", value);
editor.commit();
}
}
activity_main.xml should contain an EditText (with id = editText1) and a Button (id = button1)
Im guessing there are a few errors. But first off, you are sending a hard coded string "storedNumber", instead of getting the string from the sharedpreferences/textbox.
Change the string you are sending to something dynamic. Like the following - Which loads from textbox.
phoneNo is also hardcoded to "123" which seems unrealistic.
String phoneNo = "123";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, editText.getText().toString(), null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
Edited answer to include network selection:
package com.example.mobnet;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
public class MainActivity extends ActionBarActivity {
EditText editText;
Button saveButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
startActivity(intent);
// Find editText and saveButton
editText = (EditText) findViewById(R.id.editText1);
saveButton = (Button) findViewById(R.id.button1);
// Try to send existing number via SMS
if (sendSMS()) {
// Successful, exit the app
finish();
}
// If we are here, this means sendSMS returned false
// if the phone number was saved before, it is now displayed in editText
// add an onClickListener to the save button
saveButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// Save number and send it
savePhoneNumber(editText.getText().toString());
sendSMS();
}
});
}
/* Sends saved phone number + IMEI, IMSI via SMS, returns true if successful */
private boolean sendSMS() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String number = sharedPreferences.getString("storedNumber", "");
if (number == "") {
// No saved number, ask user to enter it and save it
Toast.makeText(this, "Enter your phone number and tap on Save after selecting network", Toast.LENGTH_SHORT).show();
return false;
}
else {
// There is saved phone number, add it to editText for later use (if SMS sending fails)
editText.setText(number);
String phoneNo = "108";
TelephonyManager tManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String Imsi=tManager.getSubscriberId();
String Imei=tManager.getDeviceId();
try{
SmsManager smsManager = SmsManager.getDefault();
String message = "IMSI, IMEI and phone number: "+Imsi+", "+Imei+", "+number;
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
return true;
}
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS failed, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
return false;
}
}
}
/* Stores phone number in the default shared prefs. */
private void savePhoneNumber(String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString("storedNumber", value);
editor.commit();
}
}

How to call BroadCast receiver class from the main activity

i'm developing an android application which will filter out the messages from a particular number and read its message body.
I have written a class which extends to broadcast receiver as follows.
package com.tutecentral.restfulapiclient;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class Filter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle extras = intent.getExtras();
if (extras != null) {
Object[] pdus = (Object[])extras.get("pdus");
if (pdus.length < 1) return;
StringBuilder sb = new StringBuilder();
String sender = null;
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (sender == null){
sender = message.getOriginatingAddress(); //get the sender
}
String text = message.getMessageBody(); //get the message body
Log.d("my broadcast","works");
//System.out.println("tsettexttt"+text);
if (text != null){
sb.append(text);
}
System.out.println("texttst"+text);
}
String number = "+94716355075"; //add the number
abortBroadcast();
}
}
}
}
Now what i want to do is to call this class from the main activity and also get the results from this class (String text : which holds the message body) so that i can use that value for other options in the app.
I went through every stackoverflow question posted regarding this but couldn't find the answer. Please be kind enough to help me out!!
Thanks in advance,
Regards
You can open your activity from the BroadcastReceiver passing some parameters, once the activity is open if certain parameters have been passed to this activity, call the method you want.
Hope it helps!
Edit: check this code out!
#Override
public void onReceive(Context context, Intent intent) {
//Do your normal work...
//start activity
Intent i = new Intent(context,YourActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("param","value"); //your parameter
context.startActivity(i);
}

Android Sim Tracker code

Hie..
I am making an app in which I am detecting whether SIM has changed or not..?
On installation of my app I am storing the current SIM serial number in shared preferences.
On boot I have registered a broadcast receiver. In onReceive() method I am accessing current SIM serial number and comparing it with the stored one on the time of installation,
Below is my code of receiver :
package com.secuirity.sms;
import org.apache.harmony.xnet.provider.jsse.GMailSender;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;
public class BootUpReciever extends BroadcastReceiver{
Context context;
String email;
String currentSimSerial;
SharedPreferences settings;
SmsManager smsMgr = SmsManager.getDefault();
public static final String PREFS_NAME = "MyPrefsFile";
#Override
public void onReceive(Context context, Intent intent) {
settings = context.getSharedPreferences(PREFS_NAME, 0);
String storedSimSerial = settings.getString("storedSimSerial", null);
Log.d("Stored Sim Serial::",storedSimSerial);
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
currentSimSerial = tm.getSimSerialNumber();
Log.d("Current Sim Serial","::"+currentSimSerial);
String trustedNum = settings.getString("cellno", null);
email = settings.getString("email", null);
if(currentSimSerial == storedSimSerial){
}else{
Log.d("Sim changed","!!!");
new GmailAsync().execute("");
String sms = "Sim card has changed, " +
"Sim Serial Number of this card is\n"+currentSimSerial+
"Network Operator"+tm.getNetworkOperatorName();
smsMgr.sendTextMessage(trustedNum, null,sms, null, null);
}
Intent sms = new Intent(context, SMSReceiver.class);
sms.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(sms);
Intent netAvailability = new Intent(context, CheckingNetworkAvailability.class);
netAvailability.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(netAvailability);
}
public class GmailAsync extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String mail_body = "Sim serial number is "+currentSimSerial;
String subject = "Your Sim has changed!!!";
GMailSender sender = new GMailSender("securemob.viasms#gmail.com", "smsfun890");
try {
sender.sendMail(subject,
mail_body+"\nThanks for using Mobile Security App",
"securemob.viasms#gmail.com",
email,null);
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.d("Mail","Sent");
}
}
}
But Its not working.... :(:(:(
I have the following permissions READ_PHONE_STATE" and "RECEIVE_BOOT_COMPLETED"
I cant see anything wrong in your code, but the first thing I would check is that you have the permission to read the SIM card set in the Manifest.
Hie Guys I got the solution of BOOT UP Receiver....
When android mobile boots then this event is broadcasts to every app, But app those are installed in the internal memory responds soon as compared to the app stored on SD card.
Hence solution was
android:installLocation="internalOnly"
In manifest file as manifest attribute.

Finish an activity,get string typed result from a class and use that result to another activity

I'm new in android environment and started a Software development project, so my knowledge is too few in it. I need help in detail.
Problem details:
Project is on ANDROID OCR code source from github Robert m.theis
currently it's outcome is - while i take an image of any written text,it retrieves quite exact output using tesseract engine as text and search in internet.
my work is -
use the text string (digits ) and call to a phone operator.
my project name is automated mobile card recharging system.
so that i took result text from a method getText() class named OcrResult.java and put into my own activity. But i don't know why this don't working in real device.
it builds, run in emulator, but in real device at least it should show messages! But it doesn't.
i also added in manifest.xml file as (angel braces are remover here)
under activity
activity android:name="edu.sfsu.cs.orange.ocr.call.CallManager"
under application
uses-permission android:name="android.permission.CALL_PHONE"
here my code is
package edu.sfsu.cs.orange.ocr.call;
import edu.sfsu.cs.orange.ocr.OcrResult;
import edu.sfsu.cs.orange.ocr.CaptureActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class CallManager extends Activity
{
public static final String preString = "*111*";
public static final String postString = "#";
//to store retrieved digits
String finalString;
//to get text result from ocr result
OcrResult getStringResult = new OcrResult();
String middleString = getStringResult.getText();
//if it fails to scan desired digit,call the process again
CaptureActivity tryProcessAgain = new CaptureActivity();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void setString(String x)
{
middleString = x;
}
public String getString( String toBeInserted)
{
if(toBeInserted.length() == 16)
{
int counter = 0;
char [] insertHere = new char[16];
for(int verifier = 0; verifier < 16; verifier ++)
{
insertHere[verifier] = toBeInserted.charAt(verifier);
if(!Character.isDigit(insertHere[verifier]))
{
break;
}
counter ++;
}
if(counter == 16)
{
finalString = preString + toBeInserted + postString;
return finalString;
}
else
{
// #SuppressWarnings("unused")
//Toast toast = Toast.makeText(this, " number scan invalid.....OCR failed. Please try again.", Toast.LENGTH_SHORT);
//toast.show();
return middleString;
}
}
else
{
//#SuppressWarnings("unused")
//Toast toast = Toast.makeText(this, " number scannin invalid...OCR failed. Please try again.", Toast.LENGTH_SHORT);
//toast.show();
return middleString;
}
}
public void CallToOperator(String callMe)
{
Toast toast = Toast.makeText(this,finalString,Toast.LENGTH_SHORT);
toast.show();
//Toast toast1 = Toast.makeText(this,middleString,Toast.LENGTH_SHORT);
//toast1.show();
if(callMe == finalString)
{
try
{
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + finalString)));
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
tryProcessAgain.onShutterButtonPressContinuous();
}
}
}

Categories

Resources