【问题标题】:Can not create Alarm Manager无法创建警报管理器
【发布时间】:2015-08-29 00:06:38
【问题描述】:

对不起,我的英语不好。我遇到了这个问题。我尝试创建持久警报通知。警报通知必须每 10 秒开始一次。我正在使用警报管理器,但它不起作用。我做错了什么?

public class RemindReceiver extends BroadcastReceiver {

private Class<?> activityClass;

public RemindReceiver() {

}

public RemindReceiver(Class<?> activityClass) {

    this.activityClass = activityClass;
}

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.mipmap.ic_launcher, "Some Text", System.currentTimeMillis());

    Intent intentTL = new Intent(context, activityClass);
    notification.setLatestEventInfo(context, "Title", "Some Text",
            PendingIntent.getActivity(context, 0, intentTL, PendingIntent.FLAG_CANCEL_CURRENT));
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    notifyManager.notify(1, notification);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5, pendingIntent);
}

public void setRemind(Context context) {

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, RemindReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pendingIntent);
}}

片段:

public class PersonListFragment extends Fragment {

private RemindReceiver remindReceiver;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.person_list_fragment_layout, container, false);

    Button nextButton = (Button) rootView.findViewById(R.id.next_button);
    ListView personListView = (ListView) rootView.findViewById(R.id.name_list_view);
    List<Person> personList = PersonListGenerator.generate();

    PersonListAdapter adapter = new PersonListAdapter(getActivity(), personList);
    personListView.setAdapter(adapter);

    Context context = getActivity().getApplicationContext();
    remindReceiver = new RemindReceiver(PersonListActivity.class);
    remindReceiver.setRemind(context);
    remindReceiver.onReceive(getActivity(), new Intent());

    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getActivity(), ExpandablePersonListActivity.class);
            startActivity(intent);
        }
    });

    return rootView;
}}

还有我的 Android 清单:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<receiver android:name=".utility.RemindReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

【问题讨论】:

  • 你说的不起作用是什么意思?你有错误吗?有什么事发生吗?
  • 什么都没发生,但我正在等待通知。

标签: java android alarmmanager android-notifications


【解决方案1】:

如果您真的想每 10 秒醒来一次,请考虑setRepeating() 处的说明:

注意:对于计时操作(滴答声、超时等),它更容易且 使用Handler 效率更高。

除此之外,如果您只用 10 秒进行测试,那么从 API 19 开始,它设计不准确

注意:从 API 19 开始,所有重复警报都是不准确的。如果你的 应用程序需要精确的交付时间,那么它必须使用一次性 准确的警报,每次重新安排如上所述。遗产 targetSdkVersion 早于 API 19 的应用程序将 继续有他们所有的警报,包括重复警报, 被视为精确。

因此,警报不会在 10 秒内发出,因为它会延迟。

对我来说,这意味着有时要等一分钟或更长时间才能听到我的闹钟,这在 API-19 之前有效。

因此,只有在您不使用 setRepeating 时,您才能期待准确的闹钟唤醒行为。

(除此之外,@Damian 的回答是正确的方向,即时间需要匹配):使用任一

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    SystemClock.elapsedRealtime() + 1000 * 5, 
    1000 * 5, 
    pendingIntent);

或者试试当前系统时间:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    System.currentTimeMillis() + 1000 * 5, 
    1000 * 5, 
    pendingIntent);

请更改您的代码以包含+1000 * 5。否则,警报会在执行时安排,因此会通过。

【讨论】:

    【解决方案2】:

    你可以试试 SystemClock.elapsedRealtime() 吗?

    例如:

        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 5, pendingIntent);
    

    我隐约记得 AlarmManager.ELAPSED_REALTIME_WAKEUP 需要 SystemClock.elapsedRealtime() 而不是 System.currentTimeMillis()

    但是我找不到我以前在哪里读过这篇文章。当我找到它时,我会更新。

    编辑:

    根据:AlarmManager.set(int type, long triggerAtMillis, PendingIntent operation) sdk docs..

    参数

    • 类型:ELAPSED_REALTIME、ELAPSED_REALTIME_WAKEUP、RTC 或 RTC_WAKEUP 之一。 triggerAtMillis 警报应该以毫秒为单位的时间 走开,使用
    • 适当:时钟(取决于闹钟类型)。
    • 操作:警报响起时执行的操作;通常来自 IntentSender.getBroadcast()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多