0

我想從Cbreceiver.class傳遞一個名爲'str'的字符串,它將廣播接收器擴展到活動cbdata.java.but但我無法傳遞值,我也沒有收到任何錯誤。謝謝you.Help我這個請從類到活動傳遞字符串

**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:** 
+0

,如果我沒看錯的Cbdata.java是一個活動,爲什麼你在那裏實現onRecieve()方法。 – 2013-03-21 04:44:14

+0

我們已經更改了代碼,從cbdata.java刪除了onReceive,我試圖做的是將字符串從Cbreceiver傳遞到cbdata並將其存儲在數據庫中 – Natto 2013-03-31 15:41:31

回答

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) 

告訴我渡過其工作或不

0

ü可以通過創建該類的對象嘗試Cbdata中的Cbreceiver ... 例如。

Cbreceiver rec = new Cbreceiver(this);

然後直接使用字符串作爲rec.str

+0

它不工作,當我嘗試訪問它使用對象時,我越來越一個空值。你可以看到在cbrecever.class中的字符串str我連接了一些變量。只有這個字符串str不工作,但是當我聲明一個字符串像字符串str =「hello」時,我能夠傳遞值。在輸入「cbreceiverobject」後訪問cbdata.java中的字符串str。在建議中,我可以看到字符串str,但它有一個奇怪的符號 - 小藍色三角形 – Natto 2013-03-31 09:01:19

相關問題