广播接收器优先级 - 仅接收一个广播 - SMS App-ANDROID

时间:2012-11-17 10:54:33

标签: android android-intent sms broadcastreceiver intentfilter

我正在开发一个短信应用程序,我很难收到短信。 我将清单中的优先级更改为

<intent-filter android:priority="2147483647" >
      <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>

BROADCAST CLASS:

public class SMSReceiver extends BroadcastReceiver {

    private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
    private SmsModel newSMS;
    private static final String TAG = "SMSReceiver";

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

    Log.i("TEST","TEST");

    /**
     *  RECEPTION SMS
     */

    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;

    if (bundle != null) {



        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];

        for (int i = 0; i < pdus.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

            from = msgs[i].getOriginatingAddress();
            content = msgs[i].getMessageBody().toString();
        }

        Log.d("FIRST BROADCAST RECIEVER", "##### incoming sms from : " + from);


            }


    this.context = context;
    bundle = intent.getExtras();
    if (bundle != null) {
        Object[] pdusObj = (Object[]) bundle.get("pdus");
        SmsMessage[] messages = new SmsMessage[pdusObj.length];

        // getting SMS information from Pdu.
        for (int i = 0; i < pdusObj.length; i++) {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
        }

        for (SmsMessage currentMessage : messages) {

            sender = currentMessage.getDisplayOriginatingAddress();
            msg = currentMessage.getDisplayMessageBody();

            Log.d("Sender::",sender);
            Log.d("Msg::",msg); 
        }



    //

    if (intent.getAction().equals(ACTION_RECEIVE_SMS)) {

        // RECUPERE SMS
            bundle = intent.getExtras();
            Object[] pdus = (Object[]) bundle.get("pdus");

            // RECONSTRUIRE SMS
            messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }

            if (messages.length > -1) {

                // AFFECTE NUM_TEL + MSG + THREAD_ID
                final String messageBody = messages[0].getMessageBody();
                final String phoneNumber = messages[0]
                        .getDisplayOriginatingAddress();
                int thread_id = ConversionFactory.getThreadIDfromNumber(phoneNumber);

                // AJOUT SMS DANS BOITE DE RECEPTION ANDROID + RECUPERATION NOUVEAU ID SMS
                ContentValues values = new ContentValues();
                values.put("address", phoneNumber);
                values.put("body", messageBody);
                Uri uri = mContext.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);

                // SI LA THREAD EXISTE...
                if(thread_id != -1)
                {
                    // CREATION NOUVEAU SMS
                    newSMS = new SmsModel(Integer.parseInt(uri.getLastPathSegment()), thread_id, messageBody, phoneNumber, 0, System.currentTimeMillis(), "receieved");

                    // AJOUTE DANS LA CONVERSATION

                    AllConversations.getInstance().getConversation(thread_id).ListeSms.put(String.valueOf(Integer.parseInt(uri.getLastPathSegment())), newSMS);

                    // SI L'UTILISATEUR EST DANS LA CONVERSATION(ACTIVTY) DU NOUVEAU SMS
                    if(thread_id == ((VariablesService)mContext.getApplicationContext()).mScheduleMessage.getItem(0).getThreadId())
                    {                           
                        // AJOUT DANS LISTVIEW MESSAGE
                        ((VariablesService)mContext.getApplicationContext()).mScheduleMessage.add(newSMS);



                    }
                    for (int i=0; i<((VariablesService)mContext.getApplicationContext()).mScheduleConversation.getCount();i++) {

                        if (((VariablesService)mContext.getApplicationContext()).mScheduleConversation.getItem(i).getThreadId() == thread_id)
                        {
                                ((VariablesService)mContext.getApplicationContext()).mScheduleConversation.remove(((VariablesService)mContext.getApplicationContext()).mScheduleConversation.getItem(i));
                                newSMS.setContactName(mContext, newSMS.getNumber());
                                ((VariablesService)mContext.getApplicationContext()).mScheduleConversation.insert(newSMS, 0);
                                Log.i("ENTRER", ((VariablesService)mContext.getApplicationContext()).mScheduleConversation.getItem(i).getMessage());                                
                        }
                    }

                    // ENVOYER BROADCAST
                    Intent returnintent=new Intent("RECIEVE_INTENT_FROM_BROADCAST");
                    mContext.sendBroadcast(returnintent);

                    Log.i(TAG, newSMS.toString());
                }

                //VIBRATOR
                Vibrator v;
                v=(Vibrator)mContext.getSystemService(Context.VIBRATOR_SERVICE);
                v.vibrate(1000);


}

XML

    <receiver
        android:name="com.application.reciever.SMSReceiver"
        class="com.application.reciever.SMSReceiver" android:exported="true">
        <intent-filter android:priority="999" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

logcat的:

11-17 11:49:08.570: I/MESSAGE(25588): Sending SMS !
11-17 11:49:08.590: I/ConversationActivity(25588): SMS Sent.
11-17 11:49:10.820: I/bcsms(25588): 3424 - 46 - tts - +33610030697 - 17-11-2012 - 1353149350823 - receieved
11-17 11:49:10.830: I/BROADCAST(25588): GOT THE INTENT
11-17 11:49:11.570: D/OpenGLRenderer(25588): Flushing caches (mode 1)
11-17 11:49:12.330: W/IInputConnectionWrapper(25588): showStatusIcon on inactive InputConnection
11-17 11:49:14.940: D/dalvikvm(25588): GC_CONCURRENT freed 293K, 8% free 8141K/8775K, paused 2ms+8ms


11-17 11:49:18.670: I/MESSAGE(25588): Sending SMS !
11-17 11:49:18.680: I/ConversationActivity(25588): SMS Sent.
11-17 11:49:21.860: D/OpenGLRenderer(25588): Flushing caches (mode 1)
11-17 11:49:22.120: W/IInputConnectionWrapper(25588): showStatusIcon on inactive InputConnection

正如您所看到的,我没有收到收件人中的第二条短信,任何想法?

4 个答案:

答案 0 :(得分:1)

我也在开发基于短信的应用程序......我没有遇到任何问题......它的工作非常好。 这是清单代码:

<receiver android:name=".SMSReceiver" >
        <intent-filter android:priority="1000" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />

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

这是在SMSReceiver.class文件中工作的一般方法:

public void onReceive(Context context, Intent intent) {

    this.context = context;
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        Object[] pdusObj = (Object[]) bundle.get("pdus");
        SmsMessage[] messages = new SmsMessage[pdusObj.length];

        // getting SMS information from Pdu.
        for (int i = 0; i < pdusObj.length; i++) {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
        }

        for (SmsMessage currentMessage : messages) {

            sender = currentMessage.getDisplayOriginatingAddress();
            msg = currentMessage.getDisplayMessageBody();

            Log.d("Sender::",sender);
            Log.d("Msg::",msg);
        }
}

答案 1 :(得分:0)

试试这个..

在您的短信广播接收器内..

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

        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;

        if (bundle != null) {

            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]);

                from = msgs[i].getOriginatingAddress();
                content = msgs[i].getMessageBody().toString();
            }

            Log.d(Constants.TAG, "##### incoming sms from : " + from);

            smsContent = "SMS received From: " + from + "\n Time: "
                    + Utils.getReadableTime(System.currentTimeMillis())
                    + "\n Content: " + content;
                }

}

答案 2 :(得分:0)

优先级值必须是整数。默认值为0.该值必须大于-1000且小于1000。

将优先级值设置为999.它将适用于您的代码。它是最高价值。

答案 3 :(得分:0)

Sms Go应用程序始终在后面运行。如果你想让你的应用程序获得短信,你应该从后台运行的服务中选择最高优先级的2运行广播接收器