SMSが将来送信されるAndroidアプリケーションを開発しようとしています。将来の日付は、ユーザーからの入力として取得されます。ここでの問題は、カウント ダウン タイマーを数日や数時間などの長期間にわたって使用する方法です。
package com.example.sked;
//import java.text.SimpleDateFormat;
import java.util.Calendar;
//import java.util.Locale;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class ScheduleActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity_schedule);
super.onCreate(savedInstanceState);
Calendar cal=Calendar.getInstance();
final int year_set=cal.get(Calendar.YEAR);
final int month_set=cal.get(Calendar.MONTH);
final int day_set=cal.get(Calendar.DAY_OF_MONTH);
final int hr_set=cal.get(Calendar.HOUR);
final int min_set=cal.get(Calendar.MINUTE);
final DatePicker dp_c = (DatePicker) findViewById(R.id.datePicker1);
final TimePicker tp_c = (TimePicker) findViewById(R.id.timePicker1);
dp_c.updateDate(year_set, month_set, day_set); //Setting current date in date picker //
tp_c.setCurrentHour(hr_set); //Setting current time in time picker //
tp_c.setCurrentMinute(min_set);
//For Scheduling a message//
final Button view = (Button) findViewById(R.id.set_message);
{
view.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// Processing here
EditText text = (EditText)findViewById(R.id.reci_number);
final String phno= text .getText().toString();
EditText text1 = (EditText)findViewById(R.id.message);
final String msg= text1.getText().toString();
Calendar cal=Calendar.getInstance();//Calculating current date in milliseconds//
final int year_curr=cal.get(Calendar.YEAR);
final int month_curr=cal.get(Calendar.MONTH);
int day_curr=cal.get(Calendar.DAY_OF_MONTH);
final int hr_curr=cal.get(Calendar.HOUR);
final int min_curr=cal.get(Calendar.MINUTE);
day_curr= year_curr*365+month_curr*30;//Converting year to days and months to days//
long day_curr_mil=TimeUnit.MILLISECONDS.convert(day_curr, TimeUnit.DAYS);
long hours_curr_mil=TimeUnit.MILLISECONDS.convert(hr_curr, TimeUnit.HOURS);
long mins_curr_mil=TimeUnit.MILLISECONDS.convert(min_curr, TimeUnit.MINUTES);
final long elapsedtimer_curr= day_curr_mil+hours_curr_mil+mins_curr_mil;
DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
int day = dp.getDayOfMonth(); //Receiving date from the user//
int month = dp.getMonth() + 1;
int year = dp.getYear();
TimePicker tp=(TimePicker) findViewById(R.id.timePicker1);
int hours= tp.getCurrentHour();//Receiving time from the user//
int minutes= tp.getCurrentMinute();
day= year*365+month*30;//Converting year to days and months to days//
long day_sel=TimeUnit.MILLISECONDS.convert(day, TimeUnit.DAYS);
long hours_sel=TimeUnit.MILLISECONDS.convert(hours, TimeUnit.HOURS);
long mins_sel=TimeUnit.MILLISECONDS.convert(minutes, TimeUnit.MINUTES);
long elapsedtimer_sel= day_sel+hours_sel+mins_sel;
if(phno.length()==10)//Checking length of the mobile number//
{
if(msg.length()<=160 && msg.length()>0) //Checking length of the message//
{
final String phoneNumber = phno;
final String message = msg;
long elapsed_act=elapsedtimer_sel-elapsedtimer_curr;
new CountDownTimer(elapsed_act, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish()
{
/*Sending the message*/
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}
}.start();
final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(ScheduleActivity.this, 0);
//set the dialog
dlgAlert.setMessage("Message successfully scheduled at the specified time & date");
dlgAlert.setTitle("Success");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
else
{
Toast.makeText(getBaseContext(),
"Message too long or too short .... cannot send ... :( ",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getBaseContext(),
"Check the number Entered",Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.schedule, menu);
return true;
}
}