大家好我有一个使用广播接收器接收短信的Android应用程序。在我的应用程序中,我使用SOAP库将SMS的数据以及收到的电话号码发送到sql server。现在我能够轻松地将数据发送到sql server直到我的android应用程序正在工作,我最小化或关闭该应用程序然后我无法调用该web服务或使用webservice将数据发送到sql server。 这是我的代码。 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();
}
}
这是我的广播接收器SMSreceiver.java类
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();
}
}
最后这是我的MainActivity.java类
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
}
}
};
现在我可以在应用程序运行时运行它。但是当我的应用程序在后台运行时,我无法发送数据。
由于