我正在使用闹钟管理器,它有单选按钮,单击单选按钮时,闹钟将推迟1,3,5,10,15和30分钟的时间间隔。
这是Fragment.class
public class DFragment extends DialogFragment{
Context context;
RadioGroup radio;
int Aplazar;
AlarmManager alarm_Manager;
PendingIntent pending_intent;
Intent mi_intent;
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_repetir, container,
false);
getDialog().setTitle("Dia de la Semana");
alarm_Manager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
final Intent mi_intent = new Intent(this.context, Alarm_Receiver.class);
radio = (RadioGroup) rootView.findViewById(R.id.RadioGroup);
Log.e("Entro a Clase Dfragment","2");
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.Rbtn) {
alarm_Manager.cancel(pending_intent);
mi_intent.putExtra("extra", "alarm_off");
context.sendBroadcast(mi_intent);
Aplazar = 1;
mi_intent.putExtra("extra", "alarm_on");
pending_intent = PendingIntent.getBroadcast(context, 0, mi_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm_Manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Aplazar * 1000, pending_intent);
} else if (checkedId == R.id.Rbtn0) {
alarm_Manager.cancel(pending_intent);
mi_intent.putExtra("extra", "alarm_off");
context.sendBroadcast(mi_intent);
Aplazar = 3;
mi_intent.putExtra("extra", "alarm_on");
pending_intent = PendingIntent.getBroadcast(context, 0, mi_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm_Manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Aplazar * 1000, pending_intent);
} else if (checkedId == R.id.Rbtn1) {
alarm_Manager.cancel(pending_intent);
mi_intent.putExtra("extra", "alarm_off");
context.sendBroadcast(mi_intent);
Aplazar = 5;
mi_intent.putExtra("extra", "alarm_on");
pending_intent = PendingIntent.getBroadcast(context, 0, mi_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm_Manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Aplazar * 1000, pending_intent);
} else if (checkedId == R.id.Rbtn2) {
alarm_Manager.cancel(pending_intent);
mi_intent.putExtra("extra", "alarm_off");
context.sendBroadcast(mi_intent);
Aplazar = 10;
mi_intent.putExtra("extra", "alarm_on");
pending_intent = PendingIntent.getBroadcast(context, 0, mi_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm_Manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Aplazar * 1000, pending_intent);
} else if (checkedId == R.id.Rbtn3) {
alarm_Manager.cancel(pending_intent);
mi_intent.putExtra("extra", "alarm_off");
context.sendBroadcast(mi_intent);
Aplazar = 15;
mi_intent.putExtra("extra", "alarm_on");
pending_intent = PendingIntent.getBroadcast(context, 0, mi_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm_Manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Aplazar * 1000, pending_intent);
} else if (checkedId == R.id.Rbtn4) {
alarm_Manager.cancel(pending_intent);
mi_intent.putExtra("extra", "alarm_off");
context.sendBroadcast(mi_intent);
Aplazar = 30;
mi_intent.putExtra("extra", "alarm_on");
pending_intent = PendingIntent.getBroadcast(context, 0, mi_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm_Manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + Aplazar * 1000, pending_intent);
}
}
});
Button Desplazar = (Button) rootView.findViewById(R.id.btnGuardarDezplazar);
final RadioButton Rbtn0 = (RadioButton) rootView.findViewById(R.id.Rbtn0);
final RadioButton Rbtn = (RadioButton) rootView.findViewById(R.id.Rbtn);
final RadioButton Rbtn1 = (RadioButton) rootView.findViewById(R.id.Rbtn1);
final RadioButton Rbtn2 = (RadioButton) rootView.findViewById(R.id.Rbtn2);
final RadioButton Rbtn3 = (RadioButton) rootView.findViewById(R.id.Rbtn3);
final RadioButton Rbtn4 = (RadioButton) rootView.findViewById(R.id.Rbtn4);
Desplazar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
return rootView;
}}
这是MainActivity.class
public class PosponerAcivity extends AppCompatActivity implements View.OnClickListener{
AlarmManager alarm_Manager;
PendingIntent pending_intent;
Intent mi_intent;
Context context;
TimePicker alarm_timepicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posponer_acivity);
alarm_Manager = (AlarmManager) getSystemService(ALARM_SERVICE);
final Intent mi_intent = new Intent(this.context, Alarm_Receiver.class);
Button Posponer = (Button) findViewById(R.id.btnPosponer);
Posponer.setOnClickListener(this);
Button Parar = (Button) findViewById(R.id.btnParar);
Parar.setOnClickListener(this);
Parar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Entro a Clase Posponer","Entro a boton PARAR");
alarm_Manager.cancel(pending_intent);
mi_intent.putExtra("extra","alarm_off");
sendBroadcast(mi_intent);
finish();
Intent intentAlarm = new Intent(getApplicationContext(), AlarmActivity.class);
startActivity(intentAlarm);
}
});
Posponer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton Rbtn0 = (RadioButton) findViewById(R.id.Rbtn0);
RadioButton Rbtn = (RadioButton) findViewById(R.id.Rbtn);
RadioButton Rbtn1 = (RadioButton) findViewById(R.id.Rbtn1);
RadioButton Rbtn2 = (RadioButton) findViewById(R.id.Rbtn2);
RadioButton Rbtn3 = (RadioButton) findViewById(R.id.Rbtn3);
RadioButton Rbtn4 = (RadioButton) findViewById(R.id.Rbtn4);
if (v.getId() == R.id.btnPosponer) {
Log.e("Entro a Clase Posponer", "Entro a boton Posponer");
try {
DFragmentDesplazar dFragmentDs = new DFragmentDesplazar();
// Show DialogFragment
dFragmentDs.show(fm, "Dialog Fragment");
} catch (Exception e) {
Log.e(null, "error");
}
}
}
});
}
FragmentManager fm = getSupportFragmentManager();
@Override
public void onClick(View v) {
}
}