发送&在Android 4.0中接收批量短信

时间:2013-04-20 15:25:17

标签: android android-intent sms

所以我几乎阅读了发送和发送的每个主题。在android中接收短信。我目前的情况是,我可以在Froyo设备上使用我的代码,因为相同的代码在具有ICS的Samsung GT P3100上失败。 失败:我可以在我的logcat中看到消息为SENT,但是当有40条消息的批量消息时,很少有随机消息丢失。在接待中观察到同样的情况。  任何形式的帮助或建议都会非常有用。以下是我的代码

的manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage"
android:versionCode="1"
android:versionName="3.2" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:resizeable="true"
    android:smallScreens="true"
    android:xlargeScreens="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <receiver android:name="com.IncomingSmsCaptureApp" >
        <intent-filter android:priority="100" >
            <action android:name="android.intent.action.DATA_SMS_RECEIVED" />

            <data android:port="5000" />
            <data android:scheme="sms" />

            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </receiver>

    <activity
        android:name="com.SplashActivity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.Activity_Login_From"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>
    <activity
        android:name="com.Activity_Main_Form"
        android:configChanges="orientation|keyboardHidden"
        android:finishOnTaskLaunch="true"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>
    <activity
        android:name="com.Activity_GatePass_Entry_Form"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>
    <activity
        android:name="com.Activity_GRN_Details_Form"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>
    <activity
        android:name="com.Farmer_Details_Activity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>
    <activity
        android:name="com.Item_Details_Activity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>
    <activity
        android:name="com.Attribute_Details_Activity"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>
    <activity
        android:name="com.Cashier_Entry_Tab"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:windowSoftInputMode="stateAlwaysHidden" >
    </activity>

</application>

发送消息的SMS.java

public class SMS {

Context mContext;
boolean smsstatus, receivedflag;
String ORG_ID, Tpin;
int is_Exception=0;
Common_Functions fun;
public static String sms_reply="false",reply_msg="";
UtilClass _util;
ProgressDialog pdauth;
public SMS(Context context) {
    mContext = context;
    fun=new Common_Functions(mContext);
    _util=new UtilClass(mContext);
}

public void sendSMS(final String phoneNumber, final String message,final String rec_no,String db_msg) {
    final String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    smsstatus = false;
    receivedflag = false;
    System.out.println("Inside Sent SMS " + phoneNumber + " " + message);


    final PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0,
            new Intent(SENT), 0);

    final PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,
            new Intent(DELIVERED), 0);

    // ---when the SMS has been sent---


    // ---when the SMS has been delivered---
//  mContext.registerReceiver(smsstatusDeliverRecver, new IntentFilter(DELIVERED));
     Thread background = new Thread(new Runnable() {
            @Override

            public void run() {

              try {
                  mContext.registerReceiver(smsstatusSentRecver, new IntentFilter(SENT));
                  Thread.sleep(2000);

                  SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage("+"+phoneNumber, null, message, sentPI,
                        deliveredPI);

                System.out.println("message sent ... " + message);


                } catch (Exception e) {

                    System.out.println("Sending Thread Exception-->>"+e.toString());

                }

            }

           });
     background.start();

    try {
        System.out.println("Calling Thread" + message);
        WaitTask wt = new WaitTask(message,rec_no,db_msg);
        wt.start();
        /*WaitTaskUser waitask=new WaitTaskUser(phoneNumber,message,rec_no,db_msg);
        waitask.execute();*/


    } catch (Exception e) {
        System.out.println("Exception  =  " + e.getMessage());
    }


}
/*BroadcastReceiver smsstatusDeliverRecver=new BroadcastReceiver() {
    public void onReceive(Context arg0, Intent arg1) {
        switch (getResultCode()) {
        case Activity.RESULT_OK:
            System.out.println("Delievery status ...RESULT_OK.... " + getResultCode());
            break;
        case Activity.RESULT_CANCELED:
            System.out.println("Delievery status ...RESULT_CANCELED.... " +getResultCode());
            break;
        }
    }
};*/

BroadcastReceiver smsstatusSentRecver=new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent arg1) {

        switch (getResultCode()) {

        case Activity.RESULT_OK:
            smsstatus = true;
            receivedflag = true;
            //_util.writeToSDFile1(message+" RESULT_OK Flag--->"+smsstatus);
            System.out.println("smsstatus :RESULT_OK " + smsstatus+"receivedflag-->>"+receivedflag);
            break;
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            smsstatus= false;
            receivedflag = true;
            //_util.writeToSDFile1(message+" RESULT_ERROR_GENERIC_FAILURE Flag--->"+smsstatus);

            System.out.println("smsstatus : RESULT_ERROR_GENERIC_FAILURE" + smsstatus+"receivedflag-->>"+receivedflag);
            // UtilClass.printsystem("result Fail : " + count);
            break;
        case SmsManager.RESULT_ERROR_NO_SERVICE:
            smsstatus = false;
            receivedflag = true;
            //_util.writeToSDFile1(message+" RESULT_ERROR_NO_SERVICE Flag--->"+smsstatus);
            System.out.println("smsstatus : RESULT_ERROR_NO_SERVICE" + smsstatus+"receivedflag-->>"+receivedflag);
            // UtilClass.printsystem("result No Service : " + count);
            break;
        case SmsManager.RESULT_ERROR_NULL_PDU:
            smsstatus = false;
            receivedflag = true;
            //_util.writeToSDFile1(message+" RESULT_ERROR_NULL_PDU:--->"+smsstatus);
            System.out.println("smsstatus :RESULT_ERROR_NULL_PDU " + smsstatus+"receivedflag-->>"+receivedflag);
            // UtilClass.printsystem("result NULL PDU : " + count);
            break;
        case SmsManager.RESULT_ERROR_RADIO_OFF:
            smsstatus = false;
            receivedflag = true;
        //  _util.writeToSDFile1(message+" RESULT_ERROR_RADIO_OFF:--->"+smsstatus);
            System.out.println("smsstatus :RESULT_ERROR_RADIO_OFF " + smsstatus+"receivedflag-->>"+receivedflag);
            // UtilClass.printsystem("result RADIO OFF : " + count);
            break;
        }
    }
};

class WaitTask extends Thread {

    String Message,rec_No,db_MSG;
    int Count;

    public WaitTask(String message,String rec_no,String db_msg) {

        Message = message;
        //Count = count;
        rec_No=rec_no;
        db_MSG=db_msg;
    }

    public void run() {

        long start = System.currentTimeMillis();
        System.out.println("start--->>"+start);
        long end = start + 60 * 100000;
        System.out.println("end--->>"+end);
        try{
        while (System.currentTimeMillis() < end) {

            if (receivedflag == true) {

                if (smsstatus == true) {
                    System.out.println("Received Response " + smsstatus);
                        sms_reply="true";
                        SQLConnect db=new SQLConnect(mContext);
                        db.open();

                        try {
                            String sb_code="";
                            if(!Message.startsWith("START"))
                            {
                                String[] arr=_util.split(Message, "#");

                                try {

                                    sb_code=Message.substring(Message.lastIndexOf("#")+1,Message.length());
                                    System.out.println("IN SMS sb_code--->>"+sb_code);
                                    int code =Integer.parseInt(sb_code,10);
                                    //  String query="delete FROM SENTBOX WHERE SENTBOX_CODE = "+code+" and TPIN = '"+_util.getUserData_atPosition(mContext,15,Activity_Login_From.Username)+"'";
                                    String query="UPDATE SENTBOX set SENT_STATUS='10' , SYNC_TYPE = 'GSM' WHERE SENTBOX_CODE = "+code+"";   
                                    System.out.println("IN SMS Delete Query--->>"+query);
                                        db.ExecuteSql(query);

                                }catch(Exception e)
                                {
                                    e.printStackTrace();
                                }
                                finally{
                                    if(db!=null)
                                        db.close();
                                }

                            }

                        }catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                        finally{
                            if(db!=null)
                                db.close();
                        }
                        System.out.println("Breaking thread: " + Message);
                    break;
                } else {

                    sms_reply="false";

                }
            }
            //mContext.unregisterReceiver(smsstatusRecver);
        }
    }
    catch (Exception e) {
        // TODO: handle exception
    }
    finally{
        if(smsstatusSentRecver.isOrderedBroadcast())
        mContext.unregisterReceiver(smsstatusSentRecver);

    }

    }

}   

}

我的接收者

public class IncomingSmsCaptureApp extends BroadcastReceiver  {
UtilClass util;
String SENT = "SMS_SENT";
String OrgId = "", CellNo = "";
Context mContext;
Common_Functions fun;

@Override
public void onReceive(Context context, Intent intent) {

    System.out.println("Message Received...");
    util = new UtilClass(context);
    fun=new Common_Functions(mContext);
    mContext=context;
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String[] Messages=null;
    String port=mContext.getString(R.string.SMS_PORT);
    String url=intent.getDataString();//SMS://Localhost:Port
    if(url.indexOf(port)!=-1)
    {

             if (bundle != null) 
             {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                Messages= new String[pdus.length];
                for (int i = 0; i < msgs.length; i++) {
                    msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                    String msg = msgs[i].getMessageBody().toString();
                    Messages[i]=msg;
                    System.out.println("Message Received..." + msg);

                }


                for(int i=0;i<Messages.length;i++)
                {
                    fun.insertIntoDb_inbox(Messages[i],"GSM",mContext);
                }
                fun.PrecessInboxMessages(mContext);

                abortBroadcast();

             }



    }

}

public boolean isDataBaseExist() {
    File dbFile = new File(Activity_Login_From.DB_PATH
            + Activity_Login_From.DatabaseName);
    System.out.print("DataBase Exist " + dbFile.exists());
    return dbFile.exists();
}

}

0 个答案:

没有答案