0

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

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

谢谢!

4

1 回答 1

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;
}
}
于 2013-10-14T20:57:59.073 回答