プログラムに問題があります。PendingIntent を作成し、AlarmManager を介してブロードキャストしました。プログラムを 2 回目に開いたときに PendingIntent に渡されるインテントからエクストラを取得したいと考えています。だから、これを手に入れるのを手伝ってください。これが私のコードです:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.silentmodeapp.R.layout.fajar);
tpStartTime = (TimePicker) findViewById(com.example.silentmodeapp.R.id.tpStartTime);
tpEndTime = (TimePicker) findViewById(com.example.silentmodeapp.R.id.tpEndTime);
setPrayerTime = (Button) findViewById(com.example.silentmodeapp.R.id.btsetTime);
cancelPrayerTime = (Button) findViewById(com.example.silentmodeapp.R.id.btcancelTime);
rbCheck=(RadioButton) findViewById(com.example.silentmodeapp.R.id.rbCheck);
//Here I want to get the Intent to use info in it.///////////////////////////////////////////////
if((PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE) != null)){
PendingIntent startIntent=(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_NO_CREATE));
rbCheck.setChecked(true);
}else {
rbCheck.setChecked(false);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(
com.example.silentmodeapp.R.menu.activity_main, menu);
return true;
}
public void setPrayerTime(View v) {
try {
Calendar calendar = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
// Set Time For Ending And Starting Time.
calendar.set(Calendar.HOUR_OF_DAY, tpStartTime.getCurrentHour());
calendar.set(Calendar.MINUTE, tpStartTime.getCurrentMinute() - 1);
calendar2.set(Calendar.HOUR_OF_DAY, tpEndTime.getCurrentHour());
calendar2.set(Calendar.MINUTE, tpEndTime.getCurrentMinute() - 1);
intent.setClass(this, SetSilentService.class);
intent.putExtra("start", "Silent Mode Started!");
intent2.setClass(this, RemoveSilentService.class);
intent2.putExtra("ended", "Silent Mode Removed!");
mp = MediaPlayer.create(Fajar.this,
com.example.silentmodeapp.R.raw.set_time);
mp.start();
pendingIntent = PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent2 = PendingIntent.getService(this, 1, intent2,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager1.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
alarmManager1.set(AlarmManager.RTC_WAKEUP,
calendar2.getTimeInMillis(), pendingIntent2);
toast.makeText(
this,
"Time is set to " + tpStartTime.getCurrentHour() + ":"
+ tpStartTime.getCurrentMinute() + " To "
+ tpEndTime.getCurrentHour() + ":"
+ tpEndTime.getCurrentMinute(), Toast.LENGTH_SHORT)
.show();
rbCheck.setChecked(true);
// startService(intent2);
} catch (Exception e) {
toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}