0
這個想法是,當用戶從他的本地電話發送短信(NOT FROM MY APP)時,我可以通知我的應用程序做一些檢查選項,然後決定是否讓用戶發送消息或停止發送消息取決於我的應用設置?有沒有辦法在用戶從OS SMS系統發送短信之前通知我的應用程序?
這個想法是,當用戶從他的本地電話發送短信(NOT FROM MY APP)時,我可以通知我的應用程序做一些檢查選項,然後決定是否讓用戶發送消息或停止發送消息取決於我的應用設置?有沒有辦法在用戶從OS SMS系統發送短信之前通知我的應用程序?
是的,你可以這樣做,默認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);
}
// 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);
}
}