Android - 使用广播接收器打开短信应用程序

时间:2013-10-14 20:50:42

标签: android sms broadcastreceiver

我需要从我的应用程序打开短信应用程序,以便用户可以发送消息,我需要获取消息的状态,即使它已被发送。有可能吗?

我知道如何定义广播接收器以编程方式发送消息,但是可以使用短信应用程序来完成吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以通过编程方式发送消息

SmsManager smsManager = SmsManager.getDefault();

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

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

对于短信接收者

公共类SmsReceiver扩展了BroadcastReceiver {

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;
}
}