捕获发送的短信(Android 2.2)

发布于 2024-12-27 06:39:37 字数 2282 浏览 2 评论 0原文

我知道这里有一些与此相关的问题,但它们都没有帮助我完成这项工作 - 捕获正在发送的短信。 我在三星手机上使用 Android 2.2 (FROYO)(如果这很重要的话)。

我在 Stackoverflow 上搜索了很多,并意识到我需要 ContentObserver 来满足我的请求。我使用的是 Service 而不是 Activity,因此我在我的 Service 类中注册了 ContentObserver,它看起来像

public class SMSSending extends Service {

private class MyContentObserver extends ContentObserver {

    public MyContentObserver() {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);      

        Uri uriSMSURI = Uri.parse("content://sms/sent");
        Cursor cur = getBaseContext().getContentResolver().query(uriSMSURI, null, null, null, null);           
        cur.moveToNext();
        String content = cur.getString(cur.getColumnIndex("body"));

        Toast.makeText(getApplicationContext(), "SOME TEXT", Toast.LENGTH_LONG).show();

    }

    @Override
    public boolean deliverSelfNotifications() {
        return false;
    }
}

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

@Override
public void onCreate() {

    MyContentObserver contentObserver = new MyContentObserver();
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    contentResolver.registerContentObserver(Uri.parse("content://sms/sent"),true, contentObserver);
    Toast.makeText(getApplicationContext(), "SERVICE CREATED", Toast.LENGTH_LONG).show();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(getApplicationContext(), "SERVICE STARTED", Toast.LENGTH_LONG).show();
}

这样 Toast 在几个地方,这样我就可以看看这是否有效 - 不幸的是,没有任何通知出现。另外,我尝试为 LogCat 添加一些代码,但没有任何反应。 我还尝试将 Uri uriSMSURI = Uri.parse("content://sms"); 而不是 content://sms/sent 但该应用程序根本不执行任何操作。 当然,我在清单中有权限:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>

我缺少什么?

I know that there are a few Questions here on SO relating to this, but none of them helped me to get this working - capture SMS that being sent.
I am using Android 2.2 (FROYO) on a Samsung phone (if that matters somehow).

I've searched a lot for this on Stackoverflow and realized that I need ContentObserver for my request. I'm using Service instead of Activity, so I've registered that ContentObserver in my Service class, and it looks like this:

public class SMSSending extends Service {

private class MyContentObserver extends ContentObserver {

    public MyContentObserver() {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);      

        Uri uriSMSURI = Uri.parse("content://sms/sent");
        Cursor cur = getBaseContext().getContentResolver().query(uriSMSURI, null, null, null, null);           
        cur.moveToNext();
        String content = cur.getString(cur.getColumnIndex("body"));

        Toast.makeText(getApplicationContext(), "SOME TEXT", Toast.LENGTH_LONG).show();

    }

    @Override
    public boolean deliverSelfNotifications() {
        return false;
    }
}

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

@Override
public void onCreate() {

    MyContentObserver contentObserver = new MyContentObserver();
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    contentResolver.registerContentObserver(Uri.parse("content://sms/sent"),true, contentObserver);
    Toast.makeText(getApplicationContext(), "SERVICE CREATED", Toast.LENGTH_LONG).show();
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(getApplicationContext(), "SERVICE STARTED", Toast.LENGTH_LONG).show();
}

}

As you can see I've put Toast in few places so I could see if this is working at all - and unfortunately none of this notifications appear. Also, i tried with putting some code for LogCat but nothing happens.
I've also tried to put Uri uriSMSURI = Uri.parse("content://sms"); instead of content://sms/sent
but the application simply doesn't do anything.
Of course, I have permissions in Manifest:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>

What am i missing?

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

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

发布评论

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

评论(1

迎风吟唱 2025-01-03 06:39:37

幸运的是,我已经设法解决了这个问题,但是使用了完全不同的方法。也许这会对将来的某人有所帮助。

我没有使用 ContentObserver (我仍然不知道为什么它不起作用),而是创建了新线程并在创建并启动我的服务后启动它。所以它看起来像这样:

...

final Uri CONTENT_URI = Uri.parse("content://sms/sent"); 

...

 public void onStart(Intent intent, int startid) { 
        Go();   
    }
    private void Go(){

        new Thread(new Runnable() { 
            public void run() { 

                try {       

                while(true){

                Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,  null); 

                if(cursor.moveToFirst()){

                    text = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 


                    if(!text.equalsIgnoreCase(actual)){
                        previous = text;
                                    //do what you need..


                    }

                }

                Thread.sleep(60000);
            }

            } catch (InterruptedException e) {

                e.printStackTrace();
            }   
            } 
    }).start(); 

它工作绝对稳定,甚至比使用 ContentObserver 更好,考虑到很多人都遇到了问题,比如 这个和其他一些..

Fortunately, I've managed to work it out, but by using totally different approach. Maybe is this going to help someone in future..

Instead of using ContentObserver (which I still don't know why didn't work) I've created new Thread and started it after my service has been created and started. So it looks like this:

...

final Uri CONTENT_URI = Uri.parse("content://sms/sent"); 

...

 public void onStart(Intent intent, int startid) { 
        Go();   
    }
    private void Go(){

        new Thread(new Runnable() { 
            public void run() { 

                try {       

                while(true){

                Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,  null); 

                if(cursor.moveToFirst()){

                    text = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 


                    if(!text.equalsIgnoreCase(actual)){
                        previous = text;
                                    //do what you need..


                    }

                }

                Thread.sleep(60000);
            }

            } catch (InterruptedException e) {

                e.printStackTrace();
            }   
            } 
    }).start(); 

It's working absolutely stable, even better than with using ContentObserver, having in mind that lot of people had problems with it, something like this and some other..

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