content:// sms / sent / not working

时间:2012-01-26 19:02:38

标签: android sms observer-pattern

这是SMS观察者代码。我需要检查只发送短信。当我使用content://sms/时,我得到了结果。但是当我使用content://sms/sent/时,为什么不能得到结果?我正在使用Android 2.1。

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class smsSentService extends Service 
{
    ContentResolver contentResolver;
    Uri uri=Uri.parse("content://sms/sent");
    Handler handler;

    @Override
    public IBinder onBind(Intent arg0) 
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        contentResolver=getContentResolver();
        contentResolver.registerContentObserver(uri, true, new contentObserver(handler));
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) 
    {
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() 
    {
        super.onDestroy();
    }

    public class contentObserver extends ContentObserver
    {
        public contentObserver(Handler handler) 
        {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) 
        {                   
            Cursor cursor = contentResolver.query(uri, null, null, null, null);
            cursor.moveToFirst();
            String content = cursor.getString(cursor.getColumnIndex("body"));
            Log.d("!!!!!!!!!!!!!", content);
            super.onChange(selfChange);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

查看http://gbandroid.googlecode.com/svn-history/r46/trunk/MobileSpy/src/org/ddth/android/monitor/observer/AndroidSmsWatcher.java

该代码侦听整个内容的更改:// sms并检查类型以查看它是否是已发送的消息。