我正在尝试在特定时间发送通知,由用户通过TimePicker
设置。
当我第一次运行我的应用程序时,它可以工作,但如果我更改通知的时间,我的应用程序会立即发送它。我无法弄清楚原因。
MainActivity代码:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pkg=getPackageName();
//Intent openPage1 = new Intent(MainActivity.this,Impostazioni.class);
//startService(new Intent(this, serviceNot.class));
Intent myIntent = new Intent(this , serviceNot.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
SharedPreferences sharedPref = getSharedPreferences("hour",MODE_PRIVATE);
notificaH = sharedPref.getInt("hour", 9);
SharedPreferences sharedPrefS = getSharedPreferences("second",MODE_PRIVATE);
notificaS = sharedPrefS.getInt("second", 10);
calendar.set(Calendar.HOUR_OF_DAY, notifyH);
calendar.set(Calendar.MINUTE, notifyS);
calendar.set(Calendar.SECOND, 00);
long time=calendar.getTimeInMillis();
if(time<System.currentTimeMillis())
{
time=time+AlarmManager.INTERVAL_DAY;
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY , pendingIntent); //set repeating every 24 hours
设置代码:
public class Impostazioni extends ActionBarActivity
{
private Switch mySwitch;
private Switch Sound;
private TextView ViewOra;
private Button btnSetOra;
int ora;
int minuti;
static final int TIME_DIALOG_ID=0;
public TimePickerDialog mTimePicker;
int notifyH=9;
int notifyS=00;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_impostazioni);
mySwitch = (Switch) findViewById(R.id.mySwitch);
Sound = (Switch) findViewById(R.id.Switch01);
ViewOra = (TextView) findViewById(R.id.ViewOra);
btnSetOra = (Button) findViewById(R.id.btnImpostaOra);
SharedPreferences sharedPref = getSharedPreferences("hour",MODE_PRIVATE);
notificaH = sharedPref.getInt("hour", 8);
SharedPreferences sharedPrefS = getSharedPreferences("second",MODE_PRIVATE);
notificaS = sharedPrefS.getInt("second", 01);
ViewOra.setText( notifyH + ":" + notifyS);
btnSetOra.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
mTimePicker = new TimePickerDialog(Impostazioni.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
ViewOra.setText( selectedHour + ":" + selectedMinute);
notifyH=selectedHour;
notifyS=selectedMinute;
SharedPreferences sharedPref = getSharedPreferences("hour",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("hour",notifyH);
prefEditor.commit();
SharedPreferences sharedPrefS = getSharedPreferences("second",MODE_PRIVATE);
SharedPreferences.Editor prefEditorS = sharedPrefS.edit();
prefEditorS.putInt("second",notifyS);
prefEditorS.commit();
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});