发送短信时接收广播

时间:2012-03-16 05:30:53

标签: android sms broadcastreceiver android-activity

当用户从Android手机发送短信时如何接收广播?我正在创建一个跟踪发送的短信和电话的应用程序。我完成了通话部分,请帮我短信。请注意,短信是通过电话发送的,而不是任何应用程序。

// ---------- -----------溶液

  public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(VIEW_RESOURCE_ID);

            SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
            ContentResolver contentResolver = this.getContentResolver();
            contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);
        }


    public class SendSmsObserver extends ContentObserver {

            public SendSmsObserver(Handler handler) {
                super(handler);
            }

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                // save the message to the SD card here

                 Log.d("sent sms", "one text send");

            }
        }

2 个答案:

答案 0 :(得分:2)

我找到了答案

public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(VIEW_RESOURCE_ID);

                SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
                ContentResolver contentResolver = this.getContentResolver();
                contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);
            }


        public class SendSmsObserver extends ContentObserver {

                public SendSmsObserver(Handler handler) {
                    super(handler);
                }

                @Override
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);
                    // save the message to the SD card here

                     Log.d("sent sms", "one text send");

                }
            }

答案 1 :(得分:1)

你可以在CallLog上构建。 CallLog提供程序包含有关已拨打和已接来电的信息。

以下代码可以正常工作

Cursor c = null; try {
    c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    if (c != null && c.moveToFirst()) {
        do {
            int duration = c.getInt(c.getColumnIndex(CallLog.Calls.DURATION));
            // do something with duration
        } while (c.moveToNext());
    } } finally {
    if (c != null) {
        c.close();
    } }

--------------------------添加新解决方案------------------ ------

看看: http://groups.google.com/group/android-developers/browse_thread/thread/9bc7d7ba0229a1d2

和: http://code.google.com/p/android/issues/detail?id=914

基本上,您可以通过在SMS上注册内容观察者来实现 消息存储。试试 这样:

  ContentResolver contentResolver = this.getContentResolver();
        contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);