簡體   English   中英

無法創建警報管理器

[英]Can not create Alarm Manager

對不起,我的英語不好。 我面對這個問題。 我嘗試創建持久警報通知。 警報通知必須每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>

您可以嘗試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操作)sdk文檔。

參量

  • 類型:ELAPSED_REALTIME,ELAPSED_REALTIME_WAKEUP,RTC或RTC_WAKEUP中的一種。 使用以下命令觸發警報的時間(以毫秒為單位)
  • 適當:時鍾(取決於警報類型)。
  • operation:警報響起時執行的操作; 通常來自IntentSender.getBroadcast()。

如果您真的想每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 否則,警報將在執行時安排,並因此通過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM