-1

Hi all i have an android application that uses broadcast receiver to receive sms. In my application i have used SOAP Library to send data of SMS as well as received phone number to sql server. Now i am able to send data to sql server easily until my android app is working as soon as i minimize or close that app then i am not able to call that webservice or send data to sql server using webservice.. here is my code. soap.class

 package com.eboss;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class Soap {

    public final String SOAP_ACTION = "http://tempuri.org/SendMessage";
    public final String OPERATION_NAME = "SendMessage";
    public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    public final String SOAP_ADDRESS = "http://192.168.1.1/service/smsservice.asmx";

    public Soap(){

    }
    public String Call(String senderno,String msgcontent) {
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
        PropertyInfo pi = new PropertyInfo();
        pi.setName("senderno");
            pi.setValue(senderno);
            pi.setType(String.class);
            request.addProperty(pi);
            pi = new PropertyInfo();
            pi.setName("msgcontent");
            pi.setValue(msgcontent);
            pi.setType(String.class);
            request.addProperty(pi);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httptransport = new HttpTransportSE(SOAP_ADDRESS);
            Object response = null;
            try {
                httptransport.call(SOAP_ACTION, envelope);
                response = envelope.getResponse();
            }
            catch (Exception e) {
                // TODO: handle exception
            response= e.toString();
            }
            return response.toString();
    }
    }

Here is my Broadcast receiver SMSreceiver.java class

    package com.eboss;

import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Bundle bundle = intent.getExtras();
        SmsMessage[] messages= null;
        //String str= "";
        String gm = "";
        String msg="";
        if (bundle!=null){
            Object[] pdus = (Object[]) bundle.get("pdus");
            messages = new SmsMessage[pdus.length];
            for(int i=0; i<messages.length;i++) {
                SmsMessage sms = SmsMessage.createFromPdu((byte[])pdus[i]);
                gm= sms.getOriginatingAddress().toString();
                msg = sms.getMessageBody().toString();
                putsmstodatabase(sms,context);
            }
            Intent broadcast = new Intent();
            broadcast.setAction("SMS_RECEIVED_ACTION");
            broadcast.putExtra("number", gm);
            broadcast.putExtra("message", msg);
            context.sendBroadcast(broadcast);           

        }
    }
    private void putsmstodatabase(SmsMessage sms, Context context) {
        // TODO Auto-generated method stub
        DataBaseHelpers helper = new DataBaseHelpers(context);
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("number", sms.getOriginatingAddress().toString());
        values.put("message", sms.getMessageBody().toString());
        db.insert("datatable", null, values);
        db.close();
    }


}

And lastly here is my MainActivity.java class

 public class MainActivity extends Activity {
    public static String rslt="";
    int count = 0;
    IntentFilter IF;
    String sm ;
    String sm2;
    DataBaseHelpers dbHelper;

    public MainActivity() {
        // TODO Auto-generated constructor stub
    }
     private BroadcastReceiver br = new BroadcastReceiver() {

        @Override
        public void onReceive(final Context context, final Intent intent) {
            final EditText et = (EditText) findViewById(R.id.editText1);
            final EditText et1 = (EditText) findViewById(R.id.editText2);
            et.setText(intent.getExtras().getString("number"));
            et1.setText(intent.getExtras().getString("message"));
            try
            { 
            rslt="START";

            sm = et.getText().toString();
            sm2 = et1.getText().toString();
            Caller c= new Caller();
            c.senderno = sm;
            c.msgcontent = sm2;
            c.join();
            c.start();
             while(rslt=="START") {
                    try {
                        Thread.sleep(10); 
                    }catch(Exception ex) {

                    }
        }
            }catch (Exception e) {
                // TODO: handle exception
            }

        }
    };

Now i am able to run it when application is working. But i am not able to send data when my application is running in background.

Thanks

user3387601
  • 59
  • 3
  • 6
  • Do you have any back ground service to send data ?? – Sree May 29 '14 at 05:58
  • i am using only webservice to send data and i am not using any background service to send data. – user3387601 May 29 '14 at 06:03
  • possible duplicate of [How can I keep my app running in the background?](http://stackoverflow.com/questions/13534463/how-can-i-keep-my-app-running-in-the-background) – Sree May 29 '14 at 06:05
  • But i have different requirement. Just help me out guyz. only want to run this webservice in background so that data can be send directly to sql server. Or please if you have any simple code then please suggest me. – user3387601 May 29 '14 at 06:17
  • why you dot search in google – Sree May 29 '14 at 06:21
  • i searched in google. but no one has such requirement to call webservice and send data to sql server in background. – user3387601 May 29 '14 at 06:28
  • plz just provide me some pseudo code. – user3387601 May 29 '14 at 08:57
  • why you dot try with your code, there is no use to wait for others help, some times there is nothing what you are looking for, but you can use many solutions to get your solution,http://stackoverflow.com/a/12457828 – Sree Jun 02 '14 at 04:28

1 Answers1

1

Use services for background processes

This is a nice tutorial

nikinci
  • 444
  • 6
  • 25
  • Thanks.But i know that i will have to use service for background but in my above code how to use services where i already have broadcast receiver to receive sms and call webservice. – user3387601 May 29 '14 at 07:30