嗨,我的代码有问题。我输入的代码是多个警报每个警报需要10秒才能触发重复。我的问题是警报只使用一个值,但虽然我已经放了一个不同的requestCode,但它不起作用或我不知道如何分别调用每个。
我试过这样做:
alarm1,值为1,请求代码为0(例如)10秒
alarm2具有不同的值和请求代码1(例如) 10秒
代码确实: 显示我最后选择的闹钟2.
static int HELLO_ID = 1;
boolean flag = false;
int CountMultipleAlarm = 0;
EditText edt,edt2;
Button btn;
CountDownTimer timer;
//the strings of the notifications
String titlePills = "Time to take Panadol",DescriptionPills = "Panadol";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
btn = (Button)findViewById(R.id.button1);
edt = (EditText)findViewById(R.id.editText1);
//hide the button and the edit text
btn.setVisibility(View.GONE);
edt.setVisibility(View.GONE);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String edittext= edt.getText().toString();
Pattern pat= Pattern.compile("[0-9]");
// Pattern pat= Pattern.compile("[a-zA-Z0-9]");
Matcher matcher = pat.matcher(edittext);
//*************Timer Start ******************
//11000 = 10 seconds(11000*6*60 == 1hour)
int count = 11000;
timer = new CountDownTimer(count, 1000)
{public void onTick(long millisUntilFinished)
{
long scnds=0;
scnds=(millisUntilFinished/1000);
}
public void onFinish()
//****** Alarm Code ********
Calendar cal = Calendar.getInstance();
// for using this you need to import java.util.Calendar;
AlarmManager am = (AlarmManager)parent.getContext().getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
//the title and the description of the notification
Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
alarmintent.putExtra("title",titlePills + "value");
alarmintent.putExtra("note","value");
//HELLO_ID is a static variable that must be initialized at the BEGINNING OF CLASS with 1;
//example:protected static int HELLO_ID =1;
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm++,
alarmintent,PendingIntent.FLAG_UPDATE_CURRENT);
//VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to
//AlarmReceiver Class
// Get the AlarmManager service
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
//Alarm coooooode end **************************************
intentArray.add(sender);
timer.start();
}
}
//************* Timer Ends *************
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}import
//Edit from Paramone: What comes after " }import " ???
alarm_Receiver类
public class Alarm_Receiver extends BroadcastReceiver {
String notification1 = "You Pills Time ";
private static int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, notification1,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, Alarm_Receiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("title");
//here we get the title and description of our Notification
String note=extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our notification
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(NOTIFICATION_ID++, notification);
}
};