2014-01-20 48 views
0

您好,我的要求是從特定的號碼接收短信。我想在textview中顯示收到的信息,但我無法從我的活動中從BroadcastReceiver中獲取值。我不知道我做mistake.please告訴我如何調用BroadcastReceiver到活動

我的代碼是

SmsReceiver

public class SmsReceiver extends BroadcastReceiver { 

public String str=null; 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 

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

      //for get sms from special number=============================== 
      String msg_from = msgs[i].getOriginatingAddress(); 
      Log.v("msg_from >>",msg_from);  
      if(msg_from.equals("XXXXXXXX")) 
      { 
       //=============================== 
      str += "SMS from " + msgs[i].getOriginatingAddress(); 
      str += " :"; 
      str += msgs[i].getMessageBody().toString(); 
      str += "\n";  
      Intent act = new Intent(context, MainActivity.class); 
      act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      act.putExtra("message", str); 
      context.startActivity(act); 

      this.abortBroadcast(); 
      } 

     } 
     // ---display the new SMS message--- 
     // Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
    } 

} 
} 

MainActivity

public class MainActivity extends Activity { 

private TextView showText; 
private String receivedSms; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent intent = getIntent(); 
    receivedSms =intent.getStringExtra("message"); 
    Log.e("mysms", receivedSms); 
    showText = (TextView)findViewById(R.id.showText); 
    showText.setText(receivedSms); 
} 

    } 
+0

發生了什麼錯誤? –

+0

當Log.e()消息打印時,如果我刪除Log.e它不顯示任何值從textview – Durga

+0

receivedSms是空? –

回答

0

添加到您的清單文件:

<uses-permission 
android:name="android.permission.RECEIVE_SMS"></uses-permission> 
<uses-permission android:name="android.permission.READ_SMS" /> 
<uses-permission 
android:name="android.permission.SEND_SMS"></uses-permission> 

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

//內部接收機類

最終捆綁捆= intent.getExtras();嘗試{

if (bundle != null) { 

    final Object[] pdusObj = (Object[]) bundle.get("pdus"); 

    for (int i = 0; i < pdusObj.length; i++) { 

     SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]); 
     String phoneNumber = currentMessage.getDisplayOriginatingAddress(); 

     String senderNum = phoneNumber; 
     String message = currentMessage.getDisplayMessageBody(); 

    } 
    } 
} 
catch (Exception e) { 
e.printstacktrac(); 
} 

將消息字符串傳遞給您的活動。

+0

謝謝,但我想讀特定號碼 – Durga

+0

的短信,然後比較您的具體號碼與收到的號碼。 – Vijju

+0

我寫了相同的代碼是什麼張貼,但在logcat Log.e(「CHEKING」,消息);沒有信息打印 – Durga

0

嘗試改變:

public String str=null; 

到:

public String str=""; 

我會建議使用StringBuffer的,而不是和字符串追加到它。

+0

不行不行 – Durga

相關問題