リマインダー アプリに 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 public class AlarmReceiver extends 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で設定した後、リマインダーを追加、編集、削除したいと思います。ありがとう