android intent短信,android – 通过Intent发送短信,并知道短信是否已被发送
在以下示例中,我們使用ContentObserver來監視SMS提供程序的更新.此Observer在SMS Intent被觸發之前創建并啟動,并根據目標地址檢查Provider更改.創建Observer的Activity必須實現SmsSendObserver.SmsSendListener接口才能接收回調.
Observer的構造函數包含一個超時參數(以毫秒為單位),如果在一段合理的時間后沒有發送消息,則允許Obse??rver正確注銷.如果需要,可以將其設置為NO_TIMEOUT.但是,如上所述,該類用于“一次性”使用,并且它將取消注冊自身并在回調時使成員無效.如果沒有回調,stop()方法可用于清理.在任何一種情況下,實例都不再可用,并且對它的任何引用都應設置為null.
示例活動:
public class MainActivity extends Activity
implements SmsSendObserver.SmsSendListener {
...
private void sendMessage(String phoneNumber, String messageBody) {
// This example has a timeout set to 15 seconds
new SmsSendObserver(this, phoneNumber, 15000).start();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("smsto:" + phoneNumber));
intent.putExtra("address", phoneNumber);
intent.putExtra("sms_body", messageBody);
intent.putExtra("exit_on_sent", true);
startActivity(intent);
}
public void onSmsSendEvent(boolean sent) {
Toast.makeText(this, sent ? "Message was sent" : "Timed out",
Toast.LENGTH_SHORT).show();
}
}
SmsSendObserver類:
public class SmsSendObserver extends ContentObserver {
public static final int NO_TIMEOUT = -1;
private static final Handler handler = new Handler();
private static final Uri uri = Uri.parse("content://sms/");
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_TYPE = "type";
private static final String[] PROJECTION = { COLUMN_ADDRESS, COLUMN_TYPE };
private static final int MESSAGE_TYPE_SENT = 2;
private Context context = null;
private ContentResolver resolver = null;
private String phoneNumber = null;
private long timeout = NO_TIMEOUT;
private boolean wasSent = false;
private boolean timedOut = false;
public SmsSendObserver(Context context, String phoneNumber, long timeout) {
super(handler);
if (context instanceof SmsSendListener) {
this.context = context;
this.resolver = context.getContentResolver();
this.phoneNumber = phoneNumber;
this.timeout = timeout;
}
else {
throw new IllegalArgumentException(
"Context must implement SmsSendListener interface");
}
}
private Runnable runOut = new Runnable() {
@Override
public void run() {
if (!wasSent) {
timedOut = true;
callBack();
}
}
};
public void start() {
if (resolver != null) {
resolver.registerContentObserver(uri, true, this);
if (timeout > NO_TIMEOUT) {
handler.postDelayed(runOut, timeout);
}
}
else {
throw new IllegalStateException(
"Current SmsSendObserver instance is invalid");
}
}
public void stop() {
if (resolver != null) {
resolver.unregisterContentObserver(this);
resolver = null;
context = null;
}
}
private void callBack() {
((SmsSendListener) context).onSmsSendEvent(wasSent);
stop();
}
@Override
public void onChange(boolean selfChange) {
if (wasSent || timedOut)
return;
Cursor cursor = null;
try {
cursor = resolver.query(uri, PROJECTION, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
final String address =
cursor.getString(cursor.getColumnIndex(COLUMN_ADDRESS));
final int type =
cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE));
if (PhoneNumberUtils.compare(address, phoneNumber) &&
type == MESSAGE_TYPE_SENT) {
wasSent = true;
callBack();
}
}
}
finally {
if (cursor != null) {
cursor.close();
}
}
}
public interface SmsSendListener {
// Passes true if the message was sent
// Passes false if timed out
public void onSmsSendEvent(boolean sent);
}
}
總結
以上是生活随笔為你收集整理的android intent短信,android – 通过Intent发送短信,并知道短信是否已被发送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言随机三位数,【分享代码】弥补c语言
- 下一篇: Android主线程耗时动画卡顿,And