我在服务中创建了一个短代码来发送短信,但是在我停止电话之前,相同的短信一直在发送。所以我在这里陷入了死亡循环......电话也是如此。
以下是代码片段:
public int onStartCommand(final Intent intent, final int flags,
final int startId) {
if (activer == 0 && activerTransmission == 1 && activerCascade == 0){
sendSms(getApplicationContext());
stopSelf();
}
if (activer == 0 && activerTransmission == 0 && activerCascade == 1){
call(getApplicationContext());
stopSelf();
}
if (activer == 0 && activerTransmission == 1 && activerCascade == 1){
sendSms(getApplicationContext());
call(getApplicationContext());
stopSelf();
}
else if (activer == 1){
Intent dialogIntent = new Intent(getBaseContext(), DialogLauncher.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
stopSelf();
}
return START_NOT_STICKY;
}
public void sendSms(Context context){
DBAdapter smsBDD = new DBAdapter(context);
Cursor sms = smsBDD.selectTransmission();
String numero1 = (sms.getString(sms.getColumnIndex("sms1")));
String numero2 = (sms.getString(sms.getColumnIndex("sms2")));
String numero3 = (sms.getString(sms.getColumnIndex("sms3")));
String numero4 = (sms.getString(sms.getColumnIndex("sms4")));
String numero5 = (sms.getString(sms.getColumnIndex("sms5")));
String msg = "Alarme perte de verticalité, ";
sms.close();
if(!numero1.equals("aucun")){
SmsManager.getDefault().sendTextMessage(numero1, null, msg+" "+getLog(context), null, null);
}
if(!numero2.equals("aucun")){
SmsManager.getDefault().sendTextMessage(numero2, null, msg/*+" "+getLog(context)*/, null, null);
}
if(!numero3.equals("aucun")){
SmsManager.getDefault().sendTextMessage(numero3, null, msg/*+" "+getLog(context)*/, null, null);
}
if(!numero4.equals("aucun")){
SmsManager.getDefault().sendTextMessage(numero4, null, msg/*+" "+getLog(context)*/, null, null);
}
if(!numero5.equals("aucun")){
SmsManager.getDefault().sendTextMessage(numero5, null, msg/*+" "+getLog(context)*/, null, null);
}
}
private void call(Context context){
DBAdapter smsBDD = new DBAdapter(context);
Cursor phone = smsBDD.selectCascade();
String numero1 = (phone.getString(phone.getColumnIndex("cascade1")));
/*String numero2 = (phone.getString(phone.getColumnIndex("cascade2")));
String numero3 = (phone.getString(phone.getColumnIndex("cascade3")));
String numero4 = (phone.getString(phone.getColumnIndex("cascade4")));
String numero5 = (phone.getString(phone.getColumnIndex("cascade5")));*/
phone.close();
if(!numero1.equals("aucun")){
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + numero1));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(intent);
}