1

我正在尝试在我的应用程序中安排短信。我正在通过 TimePicker 设置时间和通过 DatePicker 设置日期。但不知何故,代码不起作用。请告诉我哪里出错了。吨

主要代码:

    int Hour = Time_Picker.getCurrentHour();
                    int Minute = Time_Picker.getCurrentMinute();

                DatePicker Date_Picker = (DatePicker)findViewById(R.id.datePicker1);
                int day = Date_Picker.getDayOfMonth();
                 int month = Date_Picker.getMonth() + 1;
                 int year = Date_Picker.getYear();


                Intent myIntent = new Intent(ScheduleMessage.this, MyAlarmService.class);

                Bundle bundle = new Bundle();
                         bundle.putCharSequence("Number", Number.getText().toString());
                         bundle.putCharSequence("Message", Message.getText().toString());
                         myIntent.putExtras(bundle);

                pendingIntent = PendingIntent.getService(ScheduleMessage.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                 Calendar calendar = Calendar.getInstance();
                 calendar.setTimeInMillis(System.currentTimeMillis());
                 //calendar.add(Calendar.SECOND, 10);
                 calendar.set(year, month, day, Hour, Minute);

                 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

                         Toast.makeText(ScheduleMessage.this,"Start Alarm with \n" + 
                         "smsNumber = " + Number.getText().toString() + 
                         "\n" + "smsText = " + Message.getText().toString() + "\nScheduled for :"
                         + Hour +" "+Minute,
                           Toast.LENGTH_LONG).show();


`

这是服务:

public class MyAlarmService extends Service {
    String smsNumberToSend, smsTextToSend;

     @Override
     public void onCreate() {
      // TODO Auto-generated method stub

      Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
     }


     @Override
     public void onDestroy() {
      // TODO Auto-generated method stub
       super.onDestroy();
       Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
     }

     @Override
     public void onStart(Intent intent, int startId) {

         Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
      // TODO Auto-generated method stub
      //super.onStart(intent, startId);

      Bundle bundle = intent.getExtras();
           smsNumberToSend = (String) bundle.getCharSequence("Number");
           smsTextToSend = (String) bundle.getCharSequence("Message");

      Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
      Toast.makeText(this,
             "MyAlarmService.onStart() with \n" +
             "smsNumberToSend = " + smsNumberToSend + "\n" +
             "smsTextToSend = " + smsTextToSend,
             Toast.LENGTH_LONG).show();

      SmsManager smsManager = SmsManager.getDefault();
      smsManager.sendTextMessage(smsNumberToSend, null, smsTextToSend, null, null);

     }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}

有趣的是,当我添加该行时calendar.set(Calendar.SECOND,10);- 一切正常。但是当我使用相同的方法并设置特定的时间时,什么也没有发生。

请帮助我,我是Android新手。

4

2 回答 2

0

问题出在您的 BroadcastReceiver 中,您没有将捆绑包传递给服务!

public class ReceiveBroadcast extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub

    try {
        Intent ii = new Intent(arg0, AlarmService.class);
        Bundle bundle = arg1.getExtras();
        ii.putExtras(bundle);
        arg0.startService(ii);
    } catch (Exception e) {
        Toast.makeText(arg0, "There was an error somewhere, but we still received an alarm" + e, Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}
于 2014-09-13T20:45:37.220 回答
0

您应该为此目的使用广播接收器,然后您可以从那里触发您的服务。

  int Hour = Time_Picker.getCurrentHour();
                    int Minute = Time_Picker.getCurrentMinute();

                DatePicker Date_Picker = (DatePicker)findViewById(R.id.datePicker1);
                int day = Date_Picker.getDayOfMonth();
                 int month = Date_Picker.getMonth() + 1;
                 int year = Date_Picker.getYear();


                Intent myIntent = new Intent(ScheduleMessage.this, recieve.class);

                Bundle bundle = new Bundle();
                         bundle.putCharSequence("Number", Number.getText().toString());
                         bundle.putCharSequence("Message", Message.getText().toString());
                         myIntent.putExtras(bundle);

                pendingIntent = PendingIntent.getBroadcast(ScheduleMessage.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

                 Calendar calendar = Calendar.getInstance();
                 calendar.setTimeInMillis(System.currentTimeMillis());
                 //calendar.add(Calendar.SECOND, 10);
                 calendar.set(year, month, day, Hour, Minute);

                 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

                         Toast.makeText(ScheduleMessage.this,"Start Alarm with \n" + 
                         "smsNumber = " + Number.getText().toString() + 
                         "\n" + "smsText = " + Message.getText().toString() + "\nScheduled for :"
                         + Hour +" "+Minute,
                           Toast.LENGTH_LONG).show();

此代码将设置触发广播接收器的警报。现在在您的接收者的 onRecieve 函数中调用您的服务。

public class mainRecieve extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub

        try {

                    Intent ii=new Intent(arg0,MyAlarmService.class);
                    arg0.startService(ii);
                }               
                }
             catch (Exception e) {
                Toast.makeText(arg0, "There was an error somewhere, but we still received an alarm"+e, Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
    }

}
于 2013-03-05T09:40:45.787 回答