通过 contentObserver 获取外出时的短信号码
这是我从默认模拟器发送短信时尝试获取短信号码的代码,但它不起作用。 请查看并告诉我有关该信息的信息
,谢谢
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)
{}
}
}}
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)
{}
}
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请看一下这段代码: http://gbandroid.googlecode.com/svn-history/r46/trunk/MobileSpy/src/org/ddth/android/monitor/observer/AndroidSmsWatcher.java
它创建一个 ContentObserver,监听对于短信表,然后获取传出的短信并获取它们的一些信息。
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.