我有一个类,它设置了一个警报,但我需要设置大约10多个这样的报警。而不是复制类,是有办法,我可以只让类的新实例,并设定闹钟时间?
I have a class which sets an alarm but I need to set around 10 more of these alarms. Instead of duplicating classes, is there a way I can just make a new instance of the class and set the alarm time?
下面是我的code。
import java.util.Calendar; import java.lang.String; import android.app.Activity; import android.app.AlarmManager; import android.app.ListActivity; import android.app.PendingIntent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.content.Intent; import android.widget.Button; import android.widget.Toast; public class Alarm extends Activity { /* for logging - see my tutorial on debuggin Android apps for more detail */ private static final String TAG = "SomeApp "; protected Toast mToast; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setAlarm(); } public void setAlarm() { try { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_YEAR, 0); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 01); cal.set(Calendar.SECOND, 0); Intent intent = new Intent(Alarm.this, Alarm1.class); PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0); PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); // to be alerted 30 seconds from now am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sende2); // to be alerted 15 seconds from now /* To show how alarms are cancelled we will create a new Intent and a new PendingIntent with the * same requestCode as the PendingIntent alarm we want to cancel. In this case, it is 1234567. * Note: The intent and PendingIntent have to be the same as the ones used to create the alarms. */ Intent intent1 = new Intent(Alarm.this, Alarm1.class); PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0); AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE); am1.cancel(sender1); } catch (Exception e) { Log.e(TAG, "ERROR IN CODE:"+e.toString()); } } }; b1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Intent i = new Intent(getContext(),Alarm.class); //Code below is from another class which is where im calling the alarm application // ctx.startActivity (i);// start the Alarm class activity (class)public void onClick(View v) { Alarm a = new Alarm (); a.setAlarm(); b1.setText(prod); } });
以上code是从另一个类,并单击按钮,用户可以设置提醒(中BUTTOM调用报警类别,只有得到它的使用意图的工作。我只是试着拨打setAlarm方法,但没有奏效。 也许我可以做日历的一个新实例,并设置在按钮处理程序的时间。然后我会在该实例传递给报警类别。你知道,如果这将是可能的吗?
The above code is from another class and on the button click the user can set a reminder (the buttom invokes the alarm class, the only to get it to work is using an intent. I simply tried to call the setAlarm method but that didn't work. Maybe I could make a new instance of calendar and set the time in the button handler. Then I would have to pass that instance to the alarm class. Do you know if that would be possible?
推荐答案你就不能在的onCreate创建一个日历实例(),设置其参数,然后通过实例来setAlarm(),修改实例,调用setAlarm ()等,还是我失去了一些东西?
Can't you just create one Calendar instance in onCreate(), set its parameters, then pass the instance to setAlarm(), modify the instance, call setAlarm(), etc, or am I missing something?
例如。 -
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_YEAR, 0); cal.set(Calendar.HOUR_OF_DAY, 9); cal.set(Calendar.MINUTE, 01); cal.set(Calendar.SECOND, 0); setAlarm(cal); cal.set(Calendar.HOUR_OF_DAY, 12); cal.set(Calendar.MINUTE, 30); setAlarm(cal); //etc } public void setAlarm(Calendar cal) { try { Intent intent = new Intent(Alarm.this, Alarm1.class); PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0); PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); // to be alerted 30 seconds from now am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sende2); // to be alerted 15 seconds from now /* To show how alarms are cancelled we will create a new Intent and a new PendingIntent with the * same requestCode as the PendingIntent alarm we want to cancel. In this case, it is 1234567. * Note: The intent and PendingIntent have to be the same as the ones used to create the alarms. */ Intent intent1 = new Intent(Alarm.this, Alarm1.class); PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0); AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE); am1.cancel(sender1); } catch (Exception e) { Log.e(TAG, "ERROR IN CODE:"+e.toString()); } }更多推荐
Android的报警类别
发布评论