设置重复后如何管理Android AlarmManager

时间:2013-06-20 03:57:27

标签: android scheduling alarmmanager

我正在使用AlarmManager到我的提醒应用程序。 我重复提醒重复提醒。 我的MainActitity

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设置后添加,编辑,删除提醒。 感谢

0 个答案:

没有答案