get SMS number at outgoing time by contentObserver

蓝咒 提交于 2020-02-20 13:00:46

问题


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)
                        {}

                    }
        }}

回答1:


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.



来源:https://stackoverflow.com/questions/5561371/get-sms-number-at-outgoing-time-by-contentobserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!