1

アプリでテキスト メッセージのスケジュールを設定しようとしています。TimePicker を介して Time を設定し、DatePicker を介して Date を設定しています。しかし、どういうわけかコードは機能しません。どこが間違っているのか教えてください。T

メインコード:

    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

この目的のためにブロードキャストレシーバーを使用する必要があり、そこからサービスをトリガーできます。

  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 に答える
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 に答える