问题
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?
回答1:
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..
来源:https://stackoverflow.com/questions/8881173/catch-sent-sms-android-2-2