警报broadcastIntent在Android操作系统上被杀死了一段时间

时间:2015-01-14 13:03:14

标签: android alarmmanager alarm android-pendingintent

我正在开发与闹钟类似的应用程序。我使用的是Android 4.2.2。

问题是在一段时间后(大约1或2小时)警报pendingIntent会被某些东西杀死。

顺序是:
1. SetAlarm()在2小时后触发 PendingIntent alarmIntent = PendingIntent.getBroadcast(context,0,intentToFire,0); alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,time,alarmIntent);
2.我看到警报已经设置了" adb shell dumpsys alarm"。 + 1小时59分钟左 3. 10分钟后......我看到警报已经设置了" adb shell dumpsys alarm"。+ 1h49m left
2. 20分钟后......我看到警报已经设置好了" adb shell dumpsys alarm"。+ 1h39m left
等等...... 6.然后约1-2小时。 (不同时间)Oooops。 " adb shell dumpsys alarm"中没有我的报警pendingIntent。名单。

Android原生闹钟工作正常。

UPD:代码

在AndroidManifest.xml中注册的广播接收器

    <receiver android:name=".BrdRcvr" android:exported="true">
        <intent-filter>        
            <action android:name="action.alarm.RI" />
            <action android:name="action.alarm.ERT0" />
            <action android:name="action.alarm.ERT1" />
            <action android:name="action.alarm.ERT2" />
            <action android:name="action.alarm.ERT3" />
            <action android:name="action.alarm.ERT4" />
        </intent-filter>
    </receiver>

设置闹钟的功能

public static void setAlarmRI(Context context) {
    //Read time from preferences
    SharedPreferences appPref = 
            context.getSharedPreferences(SettingsActivity.APP_PREF, Context.MODE_PRIVATE);

    int timeToAlarmMs = Integer.valueOf(appPref.getInt(SettingsActivity.APP_PREF_RI, 0)) * 1000; //[ms]

    //!getSystemService must be called after onCreate() 
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    long time = SystemClock.elapsedRealtime() + timeToAlarmMs * 60; //[min]

    //Intent
    Intent intentToFire = new Intent(context, BrdRcvr.class);
    intentToFire.setAction("action.alarm.RI");

    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 
            0, intentToFire, 0/*flags*/);

    if (timeToAlarmMs > 0) {
        //for API >= 19 AlarmManager.set() triggers not at exact time (for battery saving)
        //and we should use AlarmManager.setExact() instead
        if (Build.VERSION.SDK_INT >= 19)
            alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, alarmIntent);
        else
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, alarmIntent);

        Log.d(ALARMRI, "API:" + Integer.valueOf(Build.VERSION.SDK_INT).toString());
        Log.d(ALARMRI, "set:" + Long.valueOf(time).toString());
        Log.d(ALARMRI, "cur:" + Long.valueOf(SystemClock.elapsedRealtime()).toString());
        Log.d(ALARMRI, "delta:" + (Long.valueOf(time) -
                Long.valueOf(SystemClock.elapsedRealtime())));
    } else {
        alarmManager.cancel(alarmIntent);
        Log.d(ALARMRI, "unset");
    }
}

0 个答案:

没有答案