0

我正在尝试从 Cbrceiver.class 传递一个名为“str”的字符串,该字符串将广播接收器扩展到活动 cbdata.java。但我无法传递该值,我也没有收到任何错误。谢谢。请帮帮我

**CbReceiver.class:**    


package com.example.Lovisis;

import java.io.UnsupportedEncodingException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class CbReceiver extends BroadcastReceiver
        {
        public static class SmsCbHeader {
            public static final int PDU_HEADER_LENGTH = 6;

            public final int geographicalScope;

            public final int messageCode;

            public final int updateNumber;

            public final int messageIdentifier;

            public final int dataCodingScheme;

            public final int pageIndex;

            public final int nrOfPages;

            public SmsCbHeader(byte[] pdu) throws IllegalArgumentException {
                if (pdu == null || pdu.length < PDU_HEADER_LENGTH) {
                    throw new IllegalArgumentException("Illegal PDU");
                }

                geographicalScope = (pdu[0] & 0xc0) >> 6;
                messageCode = ((pdu[0] & 0x3f) << 4) | ((pdu[1] & 0xf0) >> 4);
                updateNumber = pdu[1] & 0x0f;
                messageIdentifier = (pdu[2] << 8) | pdu[3];
                dataCodingScheme = pdu[4];


                int pageIndex = (pdu[5] & 0xf0) >> 4;
                int nrOfPages = pdu[5] & 0x0f;

                if (pageIndex == 0 || nrOfPages == 0 || pageIndex > nrOfPages) {
                    pageIndex = 1;
                    nrOfPages = 1;
                }

                this.pageIndex = pageIndex;
                this.nrOfPages = nrOfPages;
            }
    }

        public static class SmsCbMessage {

            public static final int GEOGRAPHICAL_SCOPE_CELL_WIDE_IMMEDIATE = 0;

            public static final int GEOGRAPHICAL_SCOPE_PLMN_WIDE = 1;

            public static final int GEOGRAPHICAL_SCOPE_LA_WIDE = 2;

            public static final int GEOGRAPHICAL_SCOPE_CELL_WIDE = 3;

            public static SmsCbMessage createFromPdu(byte[] pdu) {
                try {
                    return new SmsCbMessage(pdu);
                } catch (IllegalArgumentException e) {
                    return null;
                }
            }

            private String[] LANGUAGE_CODES_GROUP_0 = {
                    "de", "en", "it", "fr", "es", "nl", "sv", "da", "pt", "fi", "no", "el", "tr", "hu",
                    "pl", null
            };

            private  String[] LANGUAGE_CODES_GROUP_2 = {
                    "cs", "he", "ar", "ru", "is", null, null, null, null, null, null, null, null, null,
                    null, null
            };

            private static final char CARRIAGE_RETURN = 0x0d;

            private SmsCbHeader mHeader;

            private String mLanguage;

            private String mBody;

            private SmsCbMessage(byte[] pdu) throws IllegalArgumentException {
                mHeader = new SmsCbHeader(pdu);
                parseBody(pdu);
            }

            public int getGeographicalScope() {
                return mHeader.geographicalScope;
            }

            public String getLanguageCode() {
                return mLanguage;
            }

            public String getMessageBody() {
                return mBody;
            }

            public int getMessageIdentifier() {
                return mHeader.messageIdentifier;
            }

            public int getMessageCode() {
                return mHeader.messageCode;
            }

            public int getUpdateNumber() {
                return mHeader.updateNumber;
            }

            private void parseBody(byte[] pdu) {
                int encoding;
                boolean hasLanguageIndicator = false;

                switch ((mHeader.dataCodingScheme & 0xf0) >> 4) {
                    case 0x00:
                        encoding = SmsMessage.ENCODING_7BIT;
                        mLanguage = LANGUAGE_CODES_GROUP_0[mHeader.dataCodingScheme & 0x0f];
                        break;

                    case 0x01:
                        hasLanguageIndicator = true;
                        if ((mHeader.dataCodingScheme & 0x0f) == 0x01) {
                            encoding = SmsMessage.ENCODING_16BIT;
                        } else {
                            encoding = SmsMessage.ENCODING_7BIT;
                        }
                        break;

                    case 0x02:
                        encoding = SmsMessage.ENCODING_7BIT;
                        mLanguage = LANGUAGE_CODES_GROUP_2[mHeader.dataCodingScheme & 0x0f];
                        break;

                    case 0x03:
                        encoding = SmsMessage.ENCODING_7BIT;
                        break;

                    case 0x04:
                    case 0x05:
                        switch ((mHeader.dataCodingScheme & 0x0c) >> 2) {
                            case 0x01:
                                encoding = SmsMessage.ENCODING_8BIT;
                                break;

                            case 0x02:
                                encoding = SmsMessage.ENCODING_16BIT;
                                break;

                            case 0x00:
                            default:
                                encoding = SmsMessage.ENCODING_7BIT;
                                break;
                        }
                        break;

                    case 0x06:
                    case 0x07:

                    case 0x09:

                    case 0x0e:

                        encoding = SmsMessage.ENCODING_UNKNOWN;
                        break;

                    case 0x0f:
                        if (((mHeader.dataCodingScheme & 0x04) >> 2) == 0x01) {
                            encoding = SmsMessage.ENCODING_8BIT;
                        } else {
                            encoding = SmsMessage.ENCODING_7BIT;
                        }
                        break;

                    default:

                        encoding = SmsMessage.ENCODING_7BIT;
                        break;
                }

                switch (encoding) {
                    case SmsMessage.ENCODING_7BIT:


                        if (hasLanguageIndicator && mBody != null && mBody.length() > 2) {
                            mLanguage = mBody.substring(0, 2);
                            mBody = mBody.substring(3);
                        }
                        break;

                    case SmsMessage.ENCODING_16BIT:
                        int offset = SmsCbHeader.PDU_HEADER_LENGTH;

                        if (hasLanguageIndicator && pdu.length >= SmsCbHeader.PDU_HEADER_LENGTH + 2) {

                            offset += 2;
                        }

                        try {
                            mBody = new String(pdu, offset, (pdu.length & 0xfffe) - offset, "utf-16");
                        } catch (UnsupportedEncodingException e) {

                        }
                        break;

                    default:
                        break;
                }

                if (mBody != null) {

                    for (int i = mBody.length() - 1; i >= 0; i--) {
                        if (mBody.charAt(i) != CARRIAGE_RETURN) {
                            mBody = mBody.substring(0, i + 1);
                            break;
                        }
                    }
                } else {
                    mBody = "";
                }
            }
    }

        public String str;
        public String str1="hello";
        public void onReceive(Context context, Intent intent) {
            //---get the CB message passed in---
            Bundle bundle = intent.getExtras();        
            SmsCbMessage[] msgs = null;
            //str = "";            
            if (bundle != null)  {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsCbMessage[pdus.length];            
                for (int i=0; i<msgs.length; i++) {
                    msgs[i] = SmsCbMessage.createFromPdu((byte[])pdus[i]);                
                    str += "CB :" +msgs[i].getGeographicalScope() + msgs[i].getMessageCode() + msgs[i].getMessageIdentifier() + msgs[i].getUpdateNumber();                     

                    //Toast.makeText(context, "" +str, Toast.LENGTH_LONG).show();
                }
                Toast.makeText(context,str, Toast.LENGTH_LONG).show();
                Intent in=new Intent(CbReceiver.this,Cbdata.class);
                in.putExtra("cb", str);

                abortBroadcast();


            }                         
        }
        private Context getApplicationContext() {
            // TODO Auto-generated method stub
            return null;
        }

    }

**Cbdata.java:**
4

2 回答 2

0

删除 OnRecieve() 方法使用

 @Override
    protected void onNewIntent(Intent intent) {
    Log.d("YourActivity", "onNewIntent is called!");

    memberFieldString = intent.getStringExtra("KEY");

    super.onNewIntent(intent);
} // End of onNewIntent(Intent intent)

告诉我天气它的工作与否

于 2013-03-21T05:02:33.057 回答
0

您可以尝试在 Cbdata 中创建类 Cbrceiver 的对象...例如。

Cbrceiver rec = 新的 Cbrceiver(this);

然后直接使用字符串作为rec.str

于 2013-03-21T05:04:34.037 回答