2012-06-20 65 views
0

我正在製作擴展Broadcast Reciever的第二類SMSReciever。在這篇文章中我寫了代碼,但是在活動課上寫了些什麼。 請幫助我新來使用android。如何使用BroadcastReciever接收收到的短信

會非常感謝。

+0

你需要註冊傳入的短信,以及你需要在Android清單中設置權限, –

+0

你可以定義一個監聽器。看看[這個鏈接](http://stackoverflow.com/a/7709140/779408):) – breceivemail

回答

0

這裏是活動

receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      final Bundle bundle = intent.getExtras();   
      String str = "";    
      if (bundle != null) { 
       //---retrieve the recent SMS message received--- 
       final Object[] pdus = (Object[]) bundle.get("pdus"); 
       final SmsMessage[] msgs = new SmsMessage[pdus.length]; 

       msgs[0] = SmsMessage.createFromPdu((byte[])pdus[0]); 
       str += msgs[0].getMessageBody().toString(); 
       //---display the new SMS message--- 

       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         final TextView smsContent = (TextView) findViewById(R.id.smsContent); 
         smsContent.setText("Message : " + msgs[0].getMessageBody().toString()); 

         final TextView smsFrom = (TextView) findViewById(R.id.smsFrom); 
         smsFrom.setText("SMS from : " + msgs[0].getOriginatingAddress()); 
        } 
       }); 
      } 
     } 
    }; 
    registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED")); 

寫的代碼,這裏是SMSReceiver類代碼

@Override 
public void onReceive(Context context, Intent intent) { 
    //---get the SMS message passed in--- 
    final Bundle bundle = intent.getExtras(); 
    String str = ""; 

    if (bundle != null) { 

     //---retrieve the recent SMS message received--- 
     final Object[] pdus = (Object[]) bundle.get("pdus"); 
     final SmsMessage[] msgs = new SmsMessage[pdus.length]; 

     msgs[0] = SmsMessage.createFromPdu((byte[])pdus[0]); 
     str += "SMS from " + msgs[0].getOriginatingAddress(); 
     str += " :"; 
     str += msgs[0].getMessageBody().toString(); 
     str += "\n"; 

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

請寫下面的代碼到你的廣播接收器類,在這裏不需要發射活動。

@Override 
public void onReceive(Context context, Intent intent) { 
    //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null; 
    String str = "";    
    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]);     
       str += "SMS from " + msgs[i].getOriginatingAddress();      
       str += " :"; 
       str += msgs[i].getMessageBody().toString(); 
       str += "\n";   
     } 
     //---display the new SMS message--- 
     Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
    }  

} 

並將下面的代碼寫入您的清單文件中。

<application 
     android:icon="@drawable/icon" 
     android:label="@string/app_name" > 
     <receiver android:name="IncomingSmsCaptureApp" > 
      <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 
</application> 

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