0

这个想法是,当用户从他的本地手机(不是我的应用程序)发送短信时,我可以通知我的应用程序做一些检查选项,然后再决定是让用户发送消息还是停止发送消息取决于我的应用程序设置?

4

1 回答 1

0

是的,您可以这样做,默认情况下,Android 系统使用 SmsManager 类来发送短信。您需要实现 ContentObserver 类以获取有关外出短信的详细信息。

// 访问代码

private SmsSentObserver smsSentObserver = null;
private static final Uri STATUS_URI = Uri.parse("content://sms");

if( smsSentObserver == null )
{
    smsSentObserver = new SmsSentObserver( new Handler(), context );
    context.getContentResolver().registerContentObserver(STATUS_URI, true, smsSentObserver);
}

// 内容观察类

@SuppressLint("SimpleDateFormat")
public class SmsSentObserver extends ContentObserver 
{
    private SimpleDateFormat sdf = new SimpleDateFormat  ( "dd-MM-yyyy HH:mm:ss" );
    private static final Uri STATUS_URI = Uri.parse("content://sms");
    private Context context;

    public SmsSentObserver( Handler handler, Context context ) 
    {
        super(handler);
        this.context = context;
    }

    public boolean deliverSelfNotifications() {return true;}

    public void onChange(boolean selfChange) 
    {
        try
        {
            Cursor sms_sent_cursor = context.getContentResolver().query(STATUS_URI, null, null, null, null);
            if (sms_sent_cursor != null) 
            {
                if (sms_sent_cursor.moveToFirst()) 
                {
                    if ( sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol") ) == null  )
                    {
                        if ( sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type")) == 2 );
                        {
                            StringBuffer msgDetail = new StringBuffer();
                            msgDetail.append( "\n Sent To : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")) );
                            msgDetail.append( "\n" + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")) );
                            msgDetail.append( "\n Date : " + sdf.format( sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"))) );
                            LogFile logFile = new LogFile( context );
                            logFile.appendData( msgDetail.toString().trim() , context.getString( R.string.LogFileName ) );
                            logFile = null;
                            msgDetail = null;
                        }
                    }
                }
            }
        }
        catch( Exception e )
        {
            LogFile logFile = new LogFile( context );
            logFile.appendData( context.getString( R.string.SENT_SMS_ERROR ) + e.toString() , context.getString( R.string.LogFileName ) );
            logFile = null;
        }
        finally { System.gc(); }
        super.onChange(selfChange);
    }
}
于 2013-10-24T08:22:57.303 回答