i want to make multiple alarms with shared preferenced (because i want to edit them). this is my code and the result is that when i set 1 alarm, it works. but when i set 2 alarms,no one works (i mean that no alerts happen).what can i do with it?(i write commends for each methods). please help me,it is very necessery.thanks.
public class AlarmSet extends Activity implements View.OnClickListener {
private Intent intent1 , intent2 ;
private LinearLayout alarmLinear;
private TextView text1,text2;
private TimePicker tpTime1;
private DatePicker dpDate1;
private int hour1,minute1;
public long alarm_time1;
//shared preferenced
public SharedPreferences spTime ;
public SharedPreferences.Editor editor ;
public String req_code,rq1 ,rq2 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_set);
final Button set1 = (Button) findViewById(R.id.btn_set1);
set1.setOnClickListener(this);
final Button set2 = (Button) findViewById(R.id.btn_set2);
set2.setOnClickListener(this);
findViewById(R.id.btn_cancle1).setOnClickListener(this);
text1 = (TextView) findViewById(R.id.add1);
text1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alarmLinear.setAnimation(animRight);
alarmLinear.setVisibility(View.VISIBLE);
set1.setVisibility(View.VISIBLE);
}
});
text2 = (TextView) findViewById(R.id.add2);
text2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alarmLinear.setAnimation(animRight);
alarmLinear.setVisibility(View.VISIBLE);
set1.setVisibility(View.GONE);
set2.setVisibility(View.VISIBLE);
}
});
alarmLinear = (LinearLayout) findViewById(R.id.alarmLayout);
}
@Override
public void onClick(View view) {
switch (view.getId()){
//time picker pannel
case R.id.btn_set1:
fire_one();
intent1 = new Intent(this, alarmService.class);
intent1.putExtra("rq1", req_code);
intent1.putExtra("alarm_time", alarm_time1);
startService(intent1);
sharedPrefernces();
alarmLinear.setAnimation(animLeft);
alarmLinear.setVisibility(View.GONE);
text2.setVisibility(View.VISIBLE);
break;
case R.id.btn_set2:
fire_one();
intent2 = new Intent(this, alarmService.class);
intent2.putExtra("rq2", req_code);
intent2.putExtra("alarm_time", alarm_time1);
startService(intent2);
sharedPrefernces();
alarmLinear.setAnimation(animLeft);
alarmLinear.setVisibility(View.GONE);
case R.id.btn_cancle1:
alarmLinear.setAnimation(animLeft);
alarmLinear.setVisibility(View.GONE);
cancel_one();
break;
}
}
//set methods
private void fire_one(){
final DatePicker dpDate1 = (DatePicker) findViewById(R.id.dp_date1);
final TimePicker tpTime1 = (TimePicker) findViewById(R.id.tp_1);
int year1 = dpDate1.getYear();
int month1 = dpDate1.getMonth();
int day1 = dpDate1.getDayOfMonth();
hour1 = tpTime1.getCurrentHour();
minute1 = tpTime1.getCurrentMinute();
timeOnText(hour1, minute1);
GregorianCalendar calendar1 = new GregorianCalendar(year1,month1,day1, hour1, minute1);
//* Converting the date and time in to milliseconds elapsed since epoch
alarm_time1 = calendar1.getTimeInMillis();
Toast.makeText(getBaseContext(), "Save Time", Toast.LENGTH_SHORT).show();
}
//Shared Preferenced
public void sharedPrefernces(){
spTime = getSharedPreferences("Set Up Alarms", MODE_PRIVATE);
editor = spTime.edit();
editor.putString("add1", rq1); //put the String value in SharedPreferences and key is to retrieval of the value.
// add1 \ KEY = name of +1
//rq1 \ VALUE = request code of +1
editor.putString("add2", rq2); //put the String value in SharedPreferences and key is to retrieval of the value.
// add2 \ KEY = name of +2
//rq2 \ VALUE = request code of +2
editor.putString("add2", rq2); //put the String value in SharedPreferences and key is to retrieval of the value.
// add2 \ KEY = name of +2
//rq2 \ VALUE = request code of +2
editor.commit();// applying reflect with commit () or apply() method
}
// cancel method
private void cancel_one() {
if(intent1 != null)
stopService(intent1);
if(intent2 != null)
stopService(intent2);
}
}
0 comments:
Post a Comment