Android 结合AlarmManager浅谈Intent和PendingIntent(2)

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 private Button msetButton;
 private Button mcancelButton;
 private TextView mTextView;
 private final int REQUEST_CODE_0 = 0;
 private final int REQUEST_CODE_1 = 1;

/** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mTextView = (TextView) this.findViewById(R.id.mText);
  msetButton = (Button) this.findViewById(R.id.setTimeButton);
  mcancelButton = (Button) findViewById(R.id.cancelButton);

msetButton.setOnClickListener(new OnClickListener() {

@Override
   public void onClick(View v) {
    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getApplicationContext(),
      AlermReceiver.class);
    intent.putExtra("flag", true);
    // 创建2个PendingIntent
    PendingIntent setPendIntent0 = PendingIntent.getBroadcast(
      getApplicationContext(), REQUEST_CODE_0, intent,
      PendingIntent.FLAG_UPDATE_CURRENT);
    Log.e("FLAG_UPDATE_CURRENT-->setPendIntent0:", setPendIntent0
      + "");

PendingIntent setPendIntent1 = PendingIntent.getBroadcast(
      getApplicationContext(), REQUEST_CODE_1, intent,
      PendingIntent.FLAG_UPDATE_CURRENT);
    Log.e("FLAG_UPDATE_CURRENT-->setPendIntent1:", setPendIntent1
      + "");

// 5秒后发送广播,然后每隔10秒重复发广播
    int triggerAtTime = (int) (SystemClock.elapsedRealtime() + 5 * 1000);
    int interval = 10 * 1000;

// 注册以上创建的2个PendingIntent,每隔10秒重复发广播
    alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
      triggerAtTime, interval, setPendIntent0);
    alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
      triggerAtTime, interval, setPendIntent1);

// alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    // triggerAtTime, pendIntent1);

}
  });
  mcancelButton.setOnClickListener(new OnClickListener() {

@Override
   public void onClick(View v) {

AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getApplicationContext(),
      AlermReceiver.class);
    intent.putExtra("flag", false);
    PendingIntent StopPendIntent0 = PendingIntent.getBroadcast(
      getApplicationContext(), REQUEST_CODE_0, intent,
      PendingIntent.FLAG_UPDATE_CURRENT);

/**
    * 如果你想更新putExtra中flag的值为false,那么你必须通过alarmMgr的setxxx()方法再注册一次,
    * 相当于更新已经存在的intent,如果不这样做,你就无法达到更新的目的,像这样:
    *
    * alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0,
    * StopPendIntent0);
    *
    * 然后你再执行alarmMgr.cancel(StopPendIntent0);取消掉
    *
    * 这个可能就是你通过flag这个标志启动了一个闹钟,并响铃了,又想通过flag来关闭闹钟却咋都不能关闭的原因,
    * 问题可能就在这里, 因为实际上你没有更新数据
    */
    // alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0,
    // StopPendIntent0);

Log.e("FLAG_UPDATE_CURRENT-->StopPendIntent0:", StopPendIntent0
      + "");
    alarmMgr.cancel(StopPendIntent0);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a1e0f957efea847cb1650bd60dab5065.html