小妹現有一已建立之SQLITE 資料庫SMS TABLE名LIST 欄位_text1存各號碼
想用此來配合簡訊攔截做比對
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) {
// 系統廣播MSG;
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("來自:" + mge.getDisplayOriginatingAddress()
+ "\n");
sb.append("内容:" + 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(); // 終止BROADCAST
}
} Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG)
.show(); }
}
}
};
}
可是在接受簡訊時便當掉並結束;
有請各神人大大指導是哪裡出了問題應該怎麼改嗎?!@@
2 則回應
也許你可以參考 (不是我寫的,只是剛好找到的)
http://fecbob.pixnet.net/blog/post/38038907-android%E9%80%9A%E9%81%8Ebroadcastreceiver%E6%94%94%E6%88%AA%E7%B0%A1%E8%A8%8A
除了攔截的code還有priority要設定以及接收SMS的權限設定。
... 當在哪?
你都不看 stack trace 的嗎?