我試圖使用內容觀察者捕捉傳出的SMS事件。Android:捕捉使用ContentObserver或接收器不工作的傳出短信
//TEST OBSERVER
ContentObserver co = new SMSoutObserver(new Handler(), getApplicationContext());
ContentResolver contentResolver = getApplicationContext().getContentResolver();
contentResolver.registerContentObserver(Uri.parse("content://sms/out"),true, co);
// END TEST OBSERVER
和
public class SMSoutObserver extends ContentObserver {
private Context mCtx;
public SMSoutObserver(Handler handler, Context ctx) {
super(handler);
mCtx = ctx;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// save the message to the SD card here
Logger.d("On Change");
Toast.makeText(mCtx,"TEST", Toast.LENGTH_LONG).show();
}
}
但是,如果我在模擬器事件發送傳出消息不會被觸發。
我也嘗試過使用接收器。
<receiver android:name=".receiver.SMSReceiver"
android:enabled="true"
android:exported="true"
android:priority="1000">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.provider.Telephony.SMS_SENT"/>
</intent-filter>
</receiver>
與接收機
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Logger.d("SMSReceiver triggered");
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//do something with the received sms
Logger.d("Incoming SMS");
}else if(intent.getAction().equals("android.provider.Telephony.SMS_SENT")){
//do something with the sended sms
Logger.d("Outgoing SMS");
}
// Start reminder service
// context.startService(new Intent(context, ReminderService.class));
} catch (Exception e) {
Logger.e("onReceive method cannot be processed");
TrackingEventLogHelper.logException(e, Constants.Global.EXCEPTION,
Constants.ExceptionMessage.EXC_CANNOT_CANNOT_PROCESS_REBOOT_RECEIVER, true);
}
}
}
但這接收機僅觸發傳入的消息,而不是用於傳出。我應該如何以正確的方式在android版本> 5上進行操作。
非常感謝您的任何意見
參考http://stackoverflow.com/questions/7550178/intercepting-outgoing-sms – sasikumar
我試圖使用代碼從主題您提到,但沒有運氣.. – redrom