2012-11-17 72 views
0

我目前正在開發一個短信應用程序,我無法獲得短信的收到。 我在清單改變優先廣播接收機優先 - 只接收一個廣播 - 短信應用程序ANDROID

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

廣播類:

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 

正如你可以看到我穿上」接收第二條短信在接收者,任何想法?

+0

發佈你的BroadCastReceiver代碼。 –

+0

done,broadcast class added –

+0

您是否嘗試記錄BroadCastReciever是否被調用? –

回答

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; 
       } 

}

+0

同樣的問題:( –

+0

我試着只是後的onReceive記錄,它甚至不接收廣播的第二次... (另外,如果我改變優先級的第一次999或1000,它不接收第一廣播) –

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); 
     } 
} 
+2

這隻有當我殺GOSMS:/ –

+0

@MikeBryant是你是正確的,請嘗試以下操作: 1 - 你的Android手機 2啓動GoSMS應用 - 在'菜單 - >設置 - >接收設置' - 取消選中說'禁用其他消息通知'的框 即時通訊工作在短信應用程序MobiTexter,因爲這個GoSMS設置很多用戶認爲我們的應用程序不工作:/ –

+1

那裏雖然是因爲韓當我有gosms運行時,dcent工作,我得到兩個通知 –

0

優先級值必須是整數。默認值爲0.該值必須大於-1000且小於1000.

將優先級值設置爲999.它將適用於您的代碼。這是最高的價值。

0

短信Go應用程序始終在後面運行。如果你希望你的應用程序也可以得到短信,你應該1-從後臺運行的服務中選擇最高優先級的2-運行廣播接收器