0

我想拦截该号码保存在 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();        }
   }
}
};
}

我运行它,它不起作用〜我不知道我做错了什么,谁能给我正确的代码?我的数据库是 SMS,表名 LIST.. _text1 是数字..

4

1 回答 1

0

你创建表成功了吗?我认为表创建语句在字段之间有不需要的空间。

于 2013-10-15T06:49:17.993 回答