我一直在尝试开发一个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;
}
}
答案 0 :(得分:0)
为了您的参考帮助,我发布了这个答案。您可以从中获得帮助,因为我已经使用了时间延迟来发送通知,以便您可以修改并使用它
public class AutoNotification extends Activity {
private Timer refresh = null;
private final long refreshDelay = 5 * 1000;//this delay will be your time that the user will provide you
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_auto_notification);
sensSMSThread = new sensSMSRequest();
sensSMSThread.start();
}
class sensSMSRequest extends Thread {
boolean sendHttpRequest;
public sensSMSRequest() {
sendHttpRequest = true;
}
public void stopSendingHttpRequest() {
sendHttpRequest = false;
}
protected void onStop() {
sensSMSThread.stopSendingHttpRequest();
super.stop();
}
@Override
public void run() {
while (sendHttpRequest) {
sendSms();
SystemClock.sleep(refreshDelay);
}
}
}
}
希望以某种方式帮助你
答案 1 :(得分:0)
创建一个意图过滤器:
static {
s_intentFilter = new IntentFilter();
s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}
和广播接收器:
private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED))
{
//if(current time == sechduled time) doyourWork();
}
}
};
在服务中注册接收器:
public void onCreate() {
super.onCreate();
registerReceiver(m_timeChangedReceiver, s_intentFilter);
}
注意:不要忘记在设备重启后再次启动服务。