2017-08-01 22 views
1

這裏檢索一個SMS是我使用檢索輸入接收到的SMS!:短信代碼在服務類

//---get the SMS message passed in--- 
Bundle bundle = intent.getExtras(); 
SmsMessage[] msgs = null; 
String str = ""; 
String strd = ""; 
String strm = ""; 
if (bundle != null) 
{ 
//---retrieve the SMS message received--- 
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]); 
//if(msgs[i].getOriginatingAddress().equals("9819861968")) { 

    //---get the sender address/phone number--- 
    str += msgs[i].getOriginatingAddress(); 
    str +=":\n"; 
    strd += msgs[i].getOriginatingAddress(); 
    strd +=":\n"; 
    // } 
    //---get the message body--- 

str += msgs[i].getMessageBody().toString(); 
strm += msgs[i].getMessageBody().toString(); 

// this.abortBroadcast();

} 

現在我想獲得短信和號碼。 請幫我

回答

0

我恢復SMS很好,但沒有短信的默認應用程序!

刪除this.abortBroadcast();,這將停止發送通知給另一個正在等待這個意圖的接收者。

Official doc

我不能讓的參數傳遞給服務一流!

您可以從廣播傳遞參數雖然意圖

Intent intent = new Intent(context, [Your service].class); 
intent.put() // here you can add all the parameter 
context.startService(intent); 

和服務在您的服務器onStartCommand你會得到的意圖的數據。

+0

好的。我正在測試 – prince47

+0

我是初學者,可以給我發送從BroadcastReceiver類到服務類的參數的說明,因爲刪除this.abortBroadcast();沒有給 以及在BroadcastReceiver之前運行服務的事實不成問題?案例我甚至試圖保持SMS從BroadcastReceiver的SharedPreferences和檢索服務類,但它首先運行服務 請幫我我被解僱了一週 – prince47

+0

更新了我的答案。 –