我试图通过服务发送短信,将当前时间和日期与用户提供的时间和日期相匹配 . 当前日期,当前时间,用户提供的日期(在我的情况下为“比较”)和用户提供的时间(我的比较时间)的值case)onStartCommand函数内部正常,因为我通过日志检查它们 . 但是在onStartCommand里面,当我使用if()语句并希望将当前日期和时间与用户提供的时间和日期进行比较时,它不执行消息发送功能 . 没有if语句,如果我只想在没有比较或条件的情况下发送SMS,它就可以工作 . 您可以查看以下代码 .

public class Serviceclass extends Service {
Databaseclass dbobj;
String messagee;
String phonenumber;
String currenttime;
String comparetime;
String comparedate;
String currentdate;
@Override
public void onCreate() {
    super.onCreate();
    dbobj= new Databaseclass(getApplicationContext());
 Cursor cursor =  dbobj.getalldata();
 cursor.moveToFirst();
    final Calendar c = Calendar.getInstance();
 int  mYear = c.get(Calendar.YEAR);
  int  mMonth = c.get(Calendar.MONTH);
 int mDay = c.get(Calendar.DAY_OF_MONTH);
 int   time = c.get(Calendar.HOUR_OF_DAY);
  int   mMinute = c.get(Calendar.MINUTE);
      currentdate = mDay+"-"+(mMonth + 1)+"-"+mYear+"";
      comparedate = cursor.getString(4);
      currenttime = time + ":" + mMinute;
      comparetime = cursor.getString(3);
      phonenumber=cursor.getString(2);
      messagee=cursor.getString(1);}
  @Override
public IBinder onBind(Intent intent) {
    return null; } @Override
public void onDestroy() {
    super.onDestroy(); }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("***time(Datetosend)", comparedate);
    Log.i("***time2(currentdate)", currentdate);
    Log.i("***time3(currenttime)", currenttime);
    Log.i("***time4(timetosend)", comparetime);
    Log.i("***time4(numbertosend)", phonenumber); if(currentdate.equals(comparedate) && currenttime.equals(comparetime)) {
        SmsManager sms = SmsManager.getDefault();
        List<String> messages = sms.divideMessage(messagee);
        for (String msg : messages) {
            PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
            PendingIntent deliveredIntent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
            sms.sendTextMessage(phonenumber, null, msg, sentIntent, deliveredIntent);
            Toast.makeText(getApplicationContext(), "Text send", Toast.LENGTH_SHORT).show();
       Log.i("***time2(currentdate)", currentdate);
       }}
    return START_STICKY;
}}