我想截取短信,该号码保存在sqlite数据库中
我的代码:
public class SMS extends Activity {private static final String DBNAME = "SMS";
private static final String TABLENAME = "LIST";
private static final String FIELD01_NAME = "_id";
private static final String FIELD02_NAME = "_text1";
private SQLiteDatabase db;
private Cursor myCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = this.openOrCreateDatabase(DBNAME, MODE_APPEND, null);
String CREATE_SQL = "Create table if not exists " +
TABLENAME + " (" + FIELD01_NAME +
" integer primary key autoincrement, " +
FIELD02_NAME + " varchar not null);";
db.execSQL(CREATE_SQL);
myCursor = db.query(TABLENAME,
null, null, null, null, null, null);}
private BroadcastReceiver mBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
this.abortBroadcast();
StringBuffer sb = new StringBuffer();
String sender = null;
String content = null;
String sendtime = null;
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] mges = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
mges[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage mge : mges) {
sb.append("From:" + mge.getDisplayOriginatingAddress()
+ "\n");
sb.append("body:" + mge.getMessageBody());
sender = mge.getDisplayOriginatingAddress();
content = mge.getMessageBody();
Date date = new Date(mge.getTimestampMillis());
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
sendtime = format.format(date);
if (sender.equals( myCursor)) {
abortBroadcast();
}
} Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG)
.show(); }
}
}
};
}
我运行它并且它不起作用〜 我不知道我做错了什么,谁能给我正确的代码? 我的数据库是短信,表名LIST .. _text1是数字..
答案 0 :(得分:0)
您是否已成功创建表格?我认为表创建语句在字段之间有不需要的空间。