2011-08-19 67 views
1

我有一个setRepeating()报警计划,如果它在一定的年龄,我想在它的BroadcastReceiver禁用。我宁愿在BroadcastReceiver内禁用它,所以如果它可能是我的第一选择。如何判断Android重复闹铃的年龄?

在我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找出接收类接收广播的次数,并在每次执行警报代码时递增它,但这不是找到“年龄”的好方法。

更新:在由莫比乌斯提供的答案组合,还必须通过PendingIntent.FLAG_UPDATE_CURRENTPendingIntent.getBroadcast(),就像如下:

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

回答

1

您是否尝试过使用软件包?

内的onReceive()

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

我不知道,如果在的onReceive第二个参数()方法是来自Activity的意图。它的警报管理器正在启动接收器,而不是您的活动。如果我是对的,我的思想只有一个解决方案 - 文件存储在外部,包含闹钟的开始时间。

它是这样的:

  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))); 
    

虽然,它的CPU和电池杀手,如果你想运行 每2秒;)

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

+0

我就可以了,因为这是仅用于测试目的,其实并不重要,我如何实现它;)我只是问的问题出于好奇,在这个时候,我除了静态变量之外,没有必要实现一个解决方案..这通常不是一个可接受的解决方案,因为“过度杀伤”,而是可行的解决方案+1 – styler1972

相关问题