2013-10-14 197 views
0

我需要從我的應用程序中打開SMS應用程序,使用戶可以發送一個信息,我需要得到它,甚至發送或沒有消息的狀態打開SMS應用。可能嗎?Android的 - 與廣播接收器

我知道如何定義廣播接收機以編程方式發送郵件,但可以使用SMS的應用做呢?

謝謝!

+0

爲什麼你需要使用SMS的應用做呢? – cYrixmorten

+0

因爲我需要允許用戶選擇一個聯繫人。我會用已經寫好但沒有接收器的meesage打開短信應用程序。 – cheloperel

+0

我看到..以及棘手的部分是獲得在這種情況下,短信的狀態。如果你想使自己的活動,能夠選擇一個聯繫人,將有可能獲得發送和傳遞迴調再加上如果發生錯誤。但是,如果您撥打另一個短信應用程序,則無法保證收聽這些信息的廣播。 – cYrixmorten

回答

0

您可以發送味精編程

SmsManager smsManager = SmsManager.getDefault(); 

smsManager.sendTextMessage(phoneNo, null, sms, null, null); 

Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show(); 

短信接收器

公共類SmsReceiver擴展廣播接收器{

public static final String SMS_EXTRA_NAME = "pdus"; 
public static final String SMS_URI = "content://sms"; 

public void onReceive(Context context, Intent intent) 
{ 
    // Get SMS map from Intent 
    Bundle extras = intent.getExtras(); 

    if (extras != null) 
    { 
     // Get received SMS array 
     Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME); 

     for (int i = 0; i < smsExtra.length; ++i) 
     { 
      SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]); 

      if(IsBlackListNumber(context,sms)){ 
       this.abortBroadcast(); 
      // Toast.makeText(context, "BlackList", Toast.LENGTH_SHORT).show(); 
      } 
      else{ 
       //Toast.makeText(context, "No BlackList", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

    // WARNING!!! 
    // If you uncomment next line then received SMS will not be put to incoming. 
    // Be careful! 
    // this.abortBroadcast(); 
} 

private boolean IsBlackListNumber(Context context, SmsMessage sms){ 
     boolean isExist = false; 

    ContactInfoDataSource datasource = new ContactInfoDataSource(context); 
    datasource.read(); 
    if(datasource.IsBlackListNumber(sms.getOriginatingAddress())){ 

     SmsInfoEnt smsInfoEnt = new SmsInfoEnt(); 
     smsInfoEnt.setMessage(sms.getMessageBody()); 
     smsInfoEnt.setName(datasource.GetName(sms.getOriginatingAddress())); 
     smsInfoEnt.setPhoneNo(sms.getOriginatingAddress()); 

     SmsInfoDataSource Smsdatasource = new SmsInfoDataSource(context); 
     Smsdatasource.open(); 
     Smsdatasource.AddBlockSMS(smsInfoEnt); 
     Smsdatasource.close(); 
     isExist = true; 
    } 
    datasource.close(); 

    return isExist; 
} 
} 
+0

是的,我知道。但我需要用短信應用程序發送它,而不是編程。不管怎麼說,還是要謝謝你。 – cheloperel