通过 contentObserver 获取外出时的短信号码

发布于 2024-10-30 01:41:06 字数 2718 浏览 1 评论 0原文

这是我从默认模拟器发送短信时尝试获取短信号码的代码,但它不起作用。 请查看并告诉我有关该信息的信息

,谢谢

package com.SMSOberver5;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

public class SMSOberver5 extends Activity {
        /** Called when the activity is first created. */
    Handler handler = null;
    @Override
        public void onCreate(Bundle savedInstanceState) 
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                setReceiver();
        }
     private SMSObserver smsSentObserver = new SMSObserver(new Handler());
    private void setReceiver() {
        this.getContentResolver().registerContentObserver(
                        Uri.parse("content://sms/sent"), true, smsSentObserver);
}
class SMSObserver extends ContentObserver 
{
        public SMSObserver(Handler handler) {
        super(handler);
        }       

                    @Override
                    public void onChange(boolean selfChange) 
                    {
                        try
                        {
                         //startManagingCursor(c);
                         super.onChange(selfChange);
                         System.out.println("body");
                            System.out.println("number");
                        Cursor sms_sent_cursor = SMSOberver5.this.managedQuery(Uri
                                .parse("content://sms/sent"), null, "type=?",
                                new String[] { "2" }, null);
                        int smsEntriesCount = sms_sent_cursor.getCount();
                        String[] body = new String[smsEntriesCount];
                        String[] number = new String[smsEntriesCount];
                        if (sms_sent_cursor.moveToFirst()) 
                        {
                                for (int i = 0; i < smsEntriesCount; i++) 
                                {
                                        body[i] = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndexOrThrow("body")).toString();
                                        number[i] = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndexOrThrow("address")).toString();
                                        sms_sent_cursor.moveToNext();
                                }

                                Toast.makeText(getBaseContext(), "text", Toast.LENGTH_LONG).show();
                            }sms_sent_cursor.close();
                        }catch(Exception e)
                        {}

                    }
        }}

this is the code that i have try to get sms number when i send sms from default emulator, but it not work.
just check out and tell me about that

Thanx

package com.SMSOberver5;
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

public class SMSOberver5 extends Activity {
        /** Called when the activity is first created. */
    Handler handler = null;
    @Override
        public void onCreate(Bundle savedInstanceState) 
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                setReceiver();
        }
     private SMSObserver smsSentObserver = new SMSObserver(new Handler());
    private void setReceiver() {
        this.getContentResolver().registerContentObserver(
                        Uri.parse("content://sms/sent"), true, smsSentObserver);
}
class SMSObserver extends ContentObserver 
{
        public SMSObserver(Handler handler) {
        super(handler);
        }       

                    @Override
                    public void onChange(boolean selfChange) 
                    {
                        try
                        {
                         //startManagingCursor(c);
                         super.onChange(selfChange);
                         System.out.println("body");
                            System.out.println("number");
                        Cursor sms_sent_cursor = SMSOberver5.this.managedQuery(Uri
                                .parse("content://sms/sent"), null, "type=?",
                                new String[] { "2" }, null);
                        int smsEntriesCount = sms_sent_cursor.getCount();
                        String[] body = new String[smsEntriesCount];
                        String[] number = new String[smsEntriesCount];
                        if (sms_sent_cursor.moveToFirst()) 
                        {
                                for (int i = 0; i < smsEntriesCount; i++) 
                                {
                                        body[i] = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndexOrThrow("body")).toString();
                                        number[i] = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndexOrThrow("address")).toString();
                                        sms_sent_cursor.moveToNext();
                                }

                                Toast.makeText(getBaseContext(), "text", Toast.LENGTH_LONG).show();
                            }sms_sent_cursor.close();
                        }catch(Exception e)
                        {}

                    }
        }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2024-11-06 01:41:06

请看一下这段代码: http://gbandroid.googlecode.com/svn-history/r46/trunk/MobileSpy/src/org/ddth/android/monitor/observer/AndroidSmsWatcher.java

它创建一个 ContentObserver,监听对于短信表,然后获取传出的短信并获取它们的一些信息。

Please take a look to this code : http://gbandroid.googlecode.com/svn-history/r46/trunk/MobileSpy/src/org/ddth/android/monitor/observer/AndroidSmsWatcher.java

It creates a ContentObserver, listens for the sms table, then gets the outgoing sms and gets the several info of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文