本文介绍了捕捉发送SMS(Android 2.2的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有对SO有关这几个问题,但没有人帮助我得到这个工作 - 捕捉短信的发送。
我是一个三星手机上采用Android 2.2(升级Froyo)(如果不知何故事项)。

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).

我搜索了很多这个#2,并意识到,我需要 ContentObserver 对我的要求。我使用的是服务,而不是活动,所以我注册了 ContentObserver 在我的服务类,它看起来像这样的:

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();
}

}

正如你可以看到我已经把吐司在几个地方,所以我可以看到,如果这是工作在所有 - 不幸的是没有这个通知出现。此外,我试图把一些code为LogCat中,但没有任何反应。
我也试图把乌里uriSMSURI = Uri.parse(内容:// SMS); 而不是的内容://短信/发送
但应用程序根本没有做任何事情。
当然,我在清单的权限:

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/sentbut 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"/>

我想什么?

推荐答案

幸运的是,我已经成功地解决它,而是用完全不同的方法。也许这是怎么回事,以帮助别人的未来。

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

而不是使用ContentObserver(我仍然不知道为什么没有工作)我已经创造了新的主题,并开始是我的服务已创建并启动后。所以它看起来是这样的:

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();

它的工作绝对稳定的,甚至比使用ContentObserver更好,铭记那很多人不得不与它的问题,类似的和其他一些..

这篇关于捕捉发送SMS(Android 2.2的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 01:20