我创建了一个应用程序来读取数据库中的值并通过smsManager发送消息,它完全没问题,但突然间它停止了工作,任何人都知道原因是什么?它返回已发送状态,但实际上并未发送消息。这是代码:
package com.brs;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.telephony.SmsManager;
import android.widget.Toast;
/**
* Created by Sahil on 17-06-2015.
*/
public class Sending extends IntentService {
public static int cursor_count=0;
public Sending()
{
super("Sending");
}
SQLiteDatabase sqliteDatabase;
DatabaseHandler databasehandler;
Cursor cursor, cursor2;
protected void onHandleIntent(Intent workIntent) {
databasehandler = new DatabaseHandler(getApplicationContext());
sqliteDatabase = databasehandler.getReadableDatabase();
cursor = databasehandler.read_queue(sqliteDatabase);
cursor.moveToFirst();
System.out.println("The Vlaues for cursor : " + cursor.getCount());
//cursor_count = cursor.getCount();
cursor2 = databasehandler.read_polls(sqliteDatabase);
System.out.println("The Vlaues for cursor2 : " + cursor2.getCount());
int t = 0;
//To reach the first pending statement.
do {
String stats;
stats = cursor.getString(2);
System.out.println("The status is " + stats);
if (stats.matches("PENDING")) {
System.out.println("THE DEBUG CHECKER");
t = 1;
break;
}
} while (cursor.moveToNext());
if (t == 1)
{
do
{
String ques = "", opt1 = "", opt2 = "", opt3 = "", opt4 = "", opt5 = "", keyword = "", start_date = "", end_date = "", message = "";
int k = cursor.getInt(0);
System.out.println("The cursor index:" + k);
if (cursor2.moveToFirst())
{
do
{
int pol_id;
pol_id = cursor2.getInt(0);
if (pol_id == k)
{
ques = cursor2.getString(1);
opt1 = cursor2.getString(2);
opt2 = cursor2.getString(3);
opt3 = cursor2.getString(4);
opt4 = cursor2.getString(5);
opt5 = cursor2.getString(6);
keyword = cursor2.getString(7);
start_date = cursor2.getString(8);
end_date = cursor2.getString(9);
message = cursor2.getString(10);
break;
}
} while (cursor2.moveToNext());
}
for (int i = 0; i < 20; i++) {
int id;
String number;
String status;
id = cursor.getInt(0);
number = cursor.getString(1);
status = cursor.getString(2);
System.out.println("The id :" + id);
System.out.println("The number:" + number);
System.out.println("The status :" + status);
if (id == k) {
String sms;
if (message.matches("")) {
sms = ques + "\nA." + opt1 + "\nB." + opt2 + "\nC." + opt3 + "\nD." + opt4 + "\nE." + opt5 + "\nSTART DATE:" + start_date + "\nEND DATE:" + end_date + "\nReply in the below given format:\n" + id + " " + keyword + " (A|B|C|D|E)";
} else {
sms = message + "\nSTART DATE:" + start_date + "\nEND DATE:" + end_date + "\nReply in the below given format:\n" + id + " " + keyword + " (A|B|C|D|E)\nResponses in Invalid format would not be processed.\nSent from GOLL_POLL";
}
try {
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentPI;
String SENT = "SMS_SENT";
sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
smsManager.sendTextMessage(number, null, sms, sentPI, null);
databasehandler.update_queue(id, number, "SENT");
} catch (Exception e) {
databasehandler.update_queue(id, number, "FAIL");
System.out.println("Message sending failed");
e.printStackTrace();
}
System.out.println("The status:" + cursor.getString(2));
if (cursor.moveToNext()) {
} else {
break;
}
}
else {
break;
}
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
}
}, 40000);
cursor.moveToPrevious();
} while (cursor.moveToNext());
System.out.println("The end has been reached:");
}
}
}