好的,我得到了这项服务,它在启动时会查询数据库并通过短信从列中发出字符串。一切正常,除了文本消息继续为空
这是代码:
public class SmsFilterService extends Service{
SQLiteDatabase db;
final String dbTable = "Realtor_SMS_Table";
String messageAddress;
String messageDescription;
String messageURL;
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("SmsFilterService", "Service Started");
filterMessage(dbTable, SMSReceiver.messagefrom, SMSReceiver.messagebody);
}
public void filterMessage(String dbTable, String messagefrom,
String messagebody) {
String myPath = "/data/data/com.RealtorSMS/databases/RealtorDB.db";
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
Cursor cursor = db.query(dbTable, new String[] { "_id", "Description",
"URL" }, "_id like " + "'%" + messagebody + "%'", null, null,
null, null);
Log.d("My SMSReceiver", "sql= " + cursor);
db.close();
if (cursor != null) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
messageAddress = cursor.getString(0);
Log.d("messageAddress", messageAddress);
messageDescription = cursor.getString(1);
messageURL = cursor.getString(2);
}
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(messagefrom, null, messageAddress + " "
+ messageDescription, null, null);
sm.sendTextMessage(messagefrom, null, messageAddress + " "
+ messageURL, null, null);
}
Log.d("SmsFilterService", "Service Destroyed");
onDestroy();
}
}
我知道条目在数据库中。我的查询有问题吗?
所以我将其改为:
public void filterMessage(String dbTable, String messagefrom,
String messagebody) {
String myPath = "/data/data/com.RealtorSMS/databases/RealtorDB.db";
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
String dbquery = "select * from " + dbTable + " where _id like \'%"+messagebody+"%\'";
Cursor cursor = db.rawQuery(dbquery, null);
Log.d("My SMSReceiver", "sql= " + cursor);
db.close();
if (cursor != null) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
messageAddress = cursor.getString(0);
Log.d("messageAddress", messageAddress);
messageDescription = cursor.getString(1);
messageURL = cursor.getString(2);
}
SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage(messagefrom, null, messageAddress + " "
+ messageDescription, null, null);
sm.sendTextMessage(messagefrom, null, messageAddress + " "
+ messageURL, null, null);
}
我仍然得到空值