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