如何判断Android重复闹钟的年龄?

时间:2011-08-19 16:07:31

标签: android alarmmanager

我已安排setRepeating()警报,如果它已达到某个年龄,我想在BroadcastReceiver中禁用它。我宁愿在BroadcastReceiver内禁用它,所以如果它可能是我的第一选择。

在我的Activity

// Start our alarm
Intent intent = new Intent(Main.this, Receiver.class);

long firstTime = SystemClock.elapsedRealtime();
firstTime += 2 * 1000; // start in 2 seconds
long interval = 2000; // 2 seconds for testing

intent.putExtra("start", firstTime); // Tell the receiver the creation date (not working)

PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, 0);

// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, sender);

在我的BroadcastReceiver

@Override
public void onReceive(Context context, Intent intent) {
    // ... alarm stuff
    long now = SystemClock.elapsedRealtime();
    Log.i("", "Alarm Running for " + (now - intent.getLongExtra("start", now)));
    //getLongExtra() defaults to 'now' because there is no extra 'start'
}

在LogCat中我看到了这个..

08-19 11:01:04.420: INFO/(1371): Alarm Running for 0
08-19 11:01:06.420: INFO/(1371): Alarm Running for 0
08-19 11:01:08.430: INFO/(1371): Alarm Running for 0
08-19 11:01:10.420: INFO/(1371): Alarm Running for 0
08-19 11:01:12.419: INFO/(1371): Alarm Running for 0
08-19 11:01:14.419: INFO/(1371): Alarm Running for 0
08-19 11:01:16.420: INFO/(1371): Alarm Running for 0
08-19 11:01:18.420: INFO/(1371): Alarm Running for 0

现在这对我意味着意图不包含我用putExtra()给它的“开始”额外内容。

如何通过意图附加或其他方法告知警报的年龄?

编辑:我可以通过创建static int并在每次警报代码执行时递增它来查看接收类接收广播的次数,但这不是找到“年龄”的好方法

更新:结合Mobius提供的答案,您还必须在PendingIntent.FLAG_UPDATE_CURRENT中传递PendingIntent.getBroadcast(),如下所示:

PendingIntent sender = PendingIntent.getBroadcast(Main.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

2 个答案:

答案 0 :(得分:1)

我不确定onReceive()方法中的第二个参数是否来自Activity的意图。它是AlarmManager谁发射接收器,而不是你的活动。如果我是对的,我的想法只有一个解决方案 - 外部存储的文件,包含你的闹钟的开始时间。

它是这样的:

  1. Activity

    // Remember about adding
    // <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    // in your manifest xml
    File file = new File(Environment.getExternalStorageDirectory() + "/yourAppsName",
            ".alarmStartTime");
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(file.getAbsolutePath(), true));
        out.write(SystemClock.elapsedRealtime());
        out.close();
    } catch (IOException e) {
        Toast.makeText(this.getApplicationContext(), "Problem", Toast.LENGTH_SHORT).show();
    }
    
  2. BroadcastReceiver

    File file = new File(Environment.getExternalStorageDirectory() + "/yourAppsName",
            "alarmStartTime.txt");
    String content = "";
    try {
        content = new Scanner(file).useDelimiter("\\Z").next();
    } catch (FileNotFoundException e) {
        Toast.makeText(this.getApplicationContext(), "Problem", Toast.LENGTH_SHORT).show();
    }
    Log.i("", "Alarm Running for " + (SystemClock.elapsedRealtime() - Long.decode(content)));
    
  3. 虽然,如果你每2秒运行一次,它就是一个CPU和电池杀手;)

    再次告诉我为什么你不能使用这个静态变量?如果警报之间有特定的间隔,则迭代次数可以简单地告知您接收器的寿命。

答案 1 :(得分:1)

您是否尝试过使用捆绑包?

在onReceive()

Bundle myBundle = intent.getExtras();
    if (myBundle != null) {
        long startTime = myBundle.get("start");
}