我正在将 AlarmManager 用于我的提醒应用程序。我设置重复重复提醒。我的主要活动
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button)findViewById(R.id.button1);
final TextView tv = (TextView)findViewById(R.id.textView1);
OnClickListener click = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doAlarm(10,"Sau 10 giay");
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//tv.setText(am.toString());
}
};
btn.setOnClickListener(click);
}
private void doAlarm(int s,String _me){
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.SECOND, s);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra("alarm_message",_me);
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(MainActivity.this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
//am.setRepeating(AlarmManager.ELAPSED_REALTIME, cal.getTimeInMillis(), 10000, sender);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000,sender);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我的 AlarmReceiver 公共类 AlarmReceiver 扩展 BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
现在我想在 AlarmManager 设置后添加、编辑、删除提醒。谢谢