这个想法是,当用户从他的本机电话发送短信(不是来自我的APP)时,我可以通知我的应用程序做一些检查选项,然后我决定是否要让用户发送消息或停止发送消息取决于在我的应用程序设置?
答案 0 :(得分:0)
是的,你可以这样做,默认Android系统使用SmsManager类发送短信。您需要实现ContentObserver类以获取有关Out going sms的详细信息。
//访问代码
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);
}
// ContentObserve类
@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);
}
}