【问题标题】:Want to make my application run in background想让我的应用程序在后台运行
【发布时间】:2014-05-29 05:56:54
【问题描述】:

大家好,我有一个使用广播接收器接收短信的 android 应用程序。在我的应用程序中,我使用 SOAP 库将 SMS 数据以及接收到的电话号码发送到 sql server。现在我可以轻松地将数据发送到 sql server,直到我的 android 应用程序在我最小化或关闭该应用程序后立即运行,然后我无法调用该 web 服务或使用 web 服务将数据发送到 sql server .. 这是我的代码。 肥皂类

 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
            }

        }
    };

现在我可以在应用程序运行时运行它。但是当我的应用程序在后台运行时,我无法发送数据。

谢谢

【问题讨论】:

  • 你们有后台发送数据吗??
  • 我只使用网络服务来发送数据,我没有使用任何后台服务来发送数据。
  • 但我有不同的要求。帮帮我,伙计。只想在后台运行这个webservice,这样数据就可以直接发送到sql server。或者,如果您有任何简单的代码,请建议我。
  • 为什么要在 google 中进行点搜索

标签: android web-services sms broadcastreceiver ksoap2


【解决方案1】:

为后台进程使用服务

This is a nice tutorial

【讨论】:

  • 谢谢。但我知道我必须使用服务作为后台,但在我上面的代码中,如何使用我已经有广播接收器的服务来接收短信和调用 webservice。
猜你喜欢
  • 2015-01-04
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多