2012-01-11 56 views

回答

1

相信你能收到的消息進行廣播接收器進入的消息的每一次消息到達啓動的活動,顯示消息...

public class SMSApp extends IntentReceiver { 
    private static final String LOG_TAG = "SMSApp"; 

    /* package */ static final String ACTION = 
      "android.provider.Telephony.SMS_RECEIVED"; 

    public void onReceiveIntent(Context context, Intent intent) { 
     if (intent.getAction().equals(ACTION)) { 
      StringBuilder buf = new StringBuilder(); 
      Bundle bundle = intent.getExtras(); 
      if (bundle != null) { 
       SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent); 
       for (int i = 0; i < messages.length; i++) { 
        SmsMessage message = messages[i]; 
        buf.append("Received SMS from "); 
        buf.append(message.getDisplayOriginatingAddress()); 
        buf.append(" - "); 
        buf.append(message.getDisplayMessageBody()); 
       } 
      } 
      //start you messages activity 

     Intent i = new Intent(); 
     i.setClassName("com.test", "com.test.myMessagesAcivity"); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     //prepare message text to be sent to the activity via bundle 
     Bundle bundle = new Bundle(); 
     bundle.putString("message", but.toString()); 
     i.putExtras(bundle); 
     context.startActivity(i); 


     } 
    } 


} 

,並在您的清單文件中添加這些權限

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

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

,且該接收

<receiver class="SMSApp"> 

      <intent-filter> 

       <action android:value="android.provider.Telephony.SMS_RECEIVED" /> 

      </intent-filter> 

     </receiver> 

並從您的應用程序發送短信

使用這種方法

public void eb3atSMS(String phoneNumber, String message) 
    {   

     PendingIntent pi = PendingIntent.getActivity(this, 0, 
      new Intent(this, **DummyClasshere.class**), 0);     
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(phoneNumber, null, message, pi, null);   
    }  
+0

謝謝你,這就是我一直在尋找 – 2012-01-11 21:01:59

相關問題