Android 安卓警报类
我有一个设置警报的类,但我需要再设置大约10个警报。除了复制类,有没有一种方法可以让我创建一个新的类实例并设置报警时间 这是我的密码Android 安卓警报类,android,alarm,Android,Alarm,我有一个设置警报的类,但我需要再设置大约10个警报。除了复制类,有没有一种方法可以让我创建一个新的类实例并设置报警时间 这是我的密码 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 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);
}
});
上面的代码来自另一个类,单击按钮,用户可以设置提醒(buttom调用alarm类,唯一使其工作的方法是使用intent。我只是尝试调用setAlarm方法,但没有工作。
也许我可以创建一个新的calendar实例,并在按钮处理程序中设置时间。然后我必须将该实例传递给alarm类。您知道这是否可行吗?前台只能有一个活动,因此要调用setAlarm 10次,您需要选择另一个路径。可能是循环?;)前台只能有一个活动,因此要调用setAlarm 10次,您需要选择其他路线。也许是回路?;) 您不能在onCreate()中创建一个日历实例,设置其参数,然后将该实例传递给setAlarm(),修改该实例,调用setAlarm()等等,还是我遗漏了什么 e、 g.-
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());
}
}
您不能在onCreate()中创建一个日历实例,设置其参数,然后将该实例传递给setAlarm(),修改该实例,调用setAlarm()等等,还是我遗漏了什么 e、 g.-
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());
}
}
我在想创建一个新的类实例,不知道循环是如何工作的。我在想创建一个新的类实例,不知道循环是如何工作的。嗨,瑞克,我更新了我的原始帖子,让我对我要做的事情有了更多的了解,我认为你的想法是正确的。你是否尝试过将所有事情从上述警报中移除。OnCreate()向bl.setOnClickListener发送示例?报警a=新报警();Calendar cal=Calendar.getInstance();cal.add(日历日,年中日,0);校准设置(日历小时/天,9);校准设置(日历分钟,01);校准设置(日历秒,0);a、 设置报警(校准)。。。etcHi Rick我更新了我的原始帖子,让我更了解我要做的事情,我认为你的想法是正确的。你是否尝试过将所有内容从上述警报中移除。OnCreate()示例移到你的bl.setOnClickListener中?报警a=新报警();Calendar cal=Calendar.getInstance();cal.add(日历日,年中日,0);校准设置(日历小时/天,9);校准设置(日历分钟,01);校准设置(日历秒,0);a、 设置报警(校准)。。。等