Android studio- Infinite loop on receiving sms

128 Views Asked by At

I am trying to make a android studio project which on receiving sms will automatically reply after calculating dates using DBHandler.

  1. The programme is going on infinite loop on receiving sms.

  2. Not able to add DBHandler service to the onReceive activity.

Thanks in advance for your advise

Following is the code

SmsListener.class

public class SmsListener extends BroadcastReceiver {
private SharedPreferences preferences;
public static String msg_from = null;
public static String msgBody= null;
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
        Toast.makeText(context,"message received",Toast.LENGTH_SHORT).show();
        Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
        SmsMessage[] msgs = null;
        String format = bundle.getString("format");
        if (bundle != null){
            //---retrieve the SMS message received---
            try{
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                for(int i = 0; i<msgs.length; i++){
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                    msg_from = msgs[i].getOriginatingAddress();
                    msgBody = msgs[i].getMessageBody();
                    SmsManager sms=SmsManager.getDefault();
                    String msg=( "your appointment for ______   is on: ________" );
                    intent=new Intent(context.getApplicationContext(),MainActivity.class);
                    PendingIntent pi=PendingIntent.getActivity(context.getApplicationContext(), 0, intent,0);
                    if (msgBody.contains("Appt") ){
                        sms.sendTextMessage( msg_from, null, msg, pi,null);
                        Intent intent2 = new Intent(context,MainActivity.class);
                        context.startActivity(intent2);
                    }
                }
                Toast.makeText(context,"message is:"+msgBody+msg_from ,Toast.LENGTH_SHORT).show();
            }catch(Exception e){
                Log.d("Exception caught",e.getMessage()); }
        }
    }
   }
 }

MainActivity.class

public class MainActivity extends AppCompatActivity  {
public static String  dt,dt1, MRI1;
EditText eText,mobileno,message,number;
TextView tvw;
public static int no2=5;
public Object AdapterView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Eye();
}

public void Eye(){
    final Calendar cldr = Calendar.getInstance();
    int day = cldr.get(Calendar.DAY_OF_MONTH);
    int month = cldr.get(Calendar.MONTH);
    int year = cldr.get(Calendar.YEAR);
    Calendar c1 = Calendar.getInstance() ;
    c1.set(year, month, day);
    c1.add(Calendar.DAY_OF_MONTH, 1);
    SimpleDateFormat sdf = new SimpleDateFormat("MMM_dd_yyyy");
    DBHandler dbHandler = new DBHandler(MainActivity.this);
    dt= (sdf.format(c1.getTime()));
    int mobileno1=2;
 try {
mobileno = (EditText) findViewById(R.id.editText1);
mobileno1=Integer.valueOf(String.valueOf(mobileno));
      } catch (Exception e) {
e.printStackTrace();
 }finally {
    }
    while((dbHandler.Count_Inv(sdf.format(c1.getTime())+"_"+"Eye","Eye",MainActivity.dt, "Eye")>mobileno1) )
    {
        c1.add(Calendar.DAY_OF_MONTH, 1);
        dt= (sdf.format(c1.getTime()));
    }
    dt1 = sdf.format(c1.getTime());
    String no=SmsListener.msg_from ;
    String msgbody=SmsListener.msgBody;
    Long d =dbHandler.Count_Eye( sdf.format(c1.getTime())+"_"+"Eye","Eye", MainActivity.dt  );
    int f = d.intValue();
    double slot=0;
    switch(f){case 0:  slot= 09.00; break;case 1:slot=09.15;break;case 2:  slot= 09.30;
        case 3:  slot= 09.45; break;case 4:slot=10.00;break;case 5:  slot= 10.15;break;
    }
    dbHandler.addNewApptEye(no,dt1+"_"+"Eye",dt,dt1,d);
    MRI1 = sdf.format(c1.getTime());
    SmsManager sms=SmsManager.getDefault();
    String msg=( "your appointment for "+"Eye"+" is on:"+dt+"at"+slot+"hour");
    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
    PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
    sms.sendTextMessage( no, null, msg, pi,null);
    Toast.makeText(getApplicationContext(), "Eye"+"appointment"+MRI1,
            Toast.LENGTH_SHORT).show();}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
 }
}



  
1

There are 1 best solutions below

0
Hashim On

Solved

Added context to dbHandler as follows in SmsListener.class and started calling from SmsListener.class instead of Main_Activity.class which was causing the loop

DBHandler dbHandler = new DBHandler(context.getApplicationContext());